//
// ViewController.m
// 飞舞的UIView
//
// Created by 大欢 on 16/1/18.
// Copyright © 2016年 bjsxt. All rights reserved.
//
/********************************************************/
#import "ViewController.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
//以小写k打头定义标示符 只作用于一个编译单元
//所有字母大写 作用于全局
@interface ViewController ()
@property (nonatomic, strong) UIView * flyView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.flyView];
NSTimer * timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(flyAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(flyAction) userInfo:nil repeats:YES];
}
- (void)flyAction {
CGRect rect = self.flyView.frame;
static int X = 1;
static int Y = 1;
if (rect.origin.x < 0 || rect.origin.x + rect.size.width > SCREEN_WIDTH) {
X *= -1;
}
if (rect.origin.y < 20 || rect.origin.y + rect.size.height > SCREEN_HEIGHT) {
Y *= -1;
}
rect.origin.x += X;
rect.origin.y += Y;
self.flyView.frame = rect;
}
- (UIView *)flyView {
if (!_flyView) {
_flyView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 50, 50)];
_flyView.backgroundColor = [UIColor orangeColor];
}
return _flyView;
}
@end
/**************************************************************/