NSTimer

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
{

    NSTimer *tiemer;
    UIView *view;
    UIView *yellowView;
}
@end
static BOOL left = NO;
static BOOL right = NO;
static BOOL top = NO;
static BOOL bottom = NO;
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatView];
    
}
-(void)creatView
{
    view = [[UIView alloc]init];
    view.frame  = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+64, 0.15*self.view.frame.size.height, 0.15*self.view.frame.size.width);
    [view setBackgroundColor:[UIColor clearColor]];
    UIImage *image = [UIImage imageNamed:@"2"];
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0.15*self.view.frame.size.height, 0.15*self.view.frame.size.width)];
    [imageview setImage:image];
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 0.5;
    [view addSubview:imageview];
    [self.view addSubview:view];
    
    
    yellowView = [[UIView alloc]init];
    yellowView.frame = CGRectMake(self.view.frame.origin.x+0.15*self.view.frame.size.height+10, self.view.frame.origin.y+64,  0.15*self.view.frame.size.width,  0.15*self.view.frame.size.height);
    [yellowView setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:yellowView];
    
    UIView *topView = [[UIView alloc]init];
    topView.frame = CGRectMake(0, 0, 70, 20);
    topView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:topView];
}
-(void) updateTime
{

    CGRect rect = view.frame;
    if (right ==NO && top == NO && bottom == NO)
    {
        rect.origin.x += 5;
        if (rect.origin.x+(0.15*self.view.frame.size.width) >= 320)
        {
            
            right = YES;
        }
    }
    if (bottom == YES && left == NO)
    {
        rect.origin.x -= 5;
        rect.origin.y = self.view.frame.size.height - 0.15*self.view.frame.size.height;
        if (rect.origin.x <= 0)
        {
            left = YES;
        }
        
    }
    else if (left == YES)
    {
        rect.origin.x = 0;
        rect.origin.y -= 5;
    }
    else if (right==YES)
    {
        rect.origin.y += 5;
        rect.origin.x = 320-(0.15*self.view.frame.size.width);
        if (rect.origin.y+0.15*self.view.frame.size.height >= self.view.frame.size.height)
        {
            bottom = YES;
        }
    }
    view.frame = rect;
}

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (tiemer !=nil)
    {
        [tiemer invalidate];
        tiemer = nil;
    }
    
    tiemer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];//重複運行
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
相關文章
相關標籤/搜索