不要忘记在.h里面加上:
@property(nonatomic,strong)NSTimer *timer;
#import "ViewController.h"
@interface ViewController ()
@end
NSUInteger a;
NSUInteger b = 60;
NSUInteger c;
NSUInteger d;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(60, 60, 80, 60);
[button setTitle:@"点击看效果" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)click{
self.timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(time) userInfo:nil repeats:YES];
}
-(void)time{
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(60, 60, 70, 60);
a+=20;
if (a==180||a==220||a==300) {
b=b+60;
}
else if(a==320) {
c = label.frame.origin.x;
d = label.frame.origin.y;
[self theEnd];
}
label.frame = CGRectMake(b, 60+a, 80, 60);
label.text = @"就是这样";
[self.view addSubview:label];
}
-(void)theEnd{
UILabel *labels = [[UILabel alloc]init];
labels.frame = CGRectMake(240, 400, 70, 60);
labels.text = @"theEnd";
[self.view addSubview:labels];
[self.timer invalidate];
self.timer=nil;
}