#import “ViewController.h”
@interface ViewController ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, assign) int num;
@property (nonatomic, assign) BOOL goUp;//从下往上
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
UIView *boxView = [[UIView alloc]initWithFrame:CGRectMake((self.view.bounds.size.width–200)/2, 100, 200, 200)];
boxView.layer.borderColor = [UIColor redColor].CGColor;
boxView.layer.borderWidth = 0.5;
[self.view addSubview:boxView];
self.line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 1)];
self.line.backgroundColor = [UIColor redColor];
[boxView addSubview:self.line];
self.timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(scanAnimation) userInfo:nil repeats:YES];
}
– (void)scanAnimation {
if (self.goUp == NO) {
self.num ++;
self.line.frame = CGRectMake(0, _num, 200, 1);
if ((2*_num == 200 )) {
self.goUp = YES;
}
} else {
self.num –;
self.line.frame = CGRectMake(0, 200–_num, 200, 1);
if (self.num == 0) {
self.goUp = NO;
}
}
}