Touch事件,即鼠標處理事件,經常使用的有四種:動畫
1.觸摸開始atom
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}
2.觸摸取消code
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}
3.觸摸結束事件
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}
4.觸摸移動ip
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}
5.多點觸摸,要進行以下設置animation
[self.view setMultipleTouchEnabled:YES];
一個簡單的小應用:視圖隨鼠標移動的簡單動畫it
@interface ViewController () @property (nonatomic, strong) UIView *animationView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"%d", [_animationView retainCount]); _animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 100, 100)]; NSLog(@"%d", [_animationView retainCount]); [_animationView setBackgroundColor:[UIColor lightGrayColor]]; [self.view addSubview:_animationView]; } //點擊開始 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //獲取touch事件 UITouch *touch = [touches anyObject]; //獲取觸摸點的座標 CGPoint touchPoint = [touch locationInView:self.view]; [_animationView setCenter:touchPoint]; } //點擊結束 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"cancelled"); } //觸摸結束 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"ended"); } //鼠標開始移動 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"moved"); UITouch *touch = [touches anyObject]; //獲取觸摸點的座標 CGPoint touchPoint = [touch locationInView:self.view]; // NSLog(@"x:%.1f y:%.1f", touchPoint.x, touchPoint.y); //獲取灰色視圖 //思路:animationView是self.view的子視圖,就能夠經過self.view獲得他自己的子視圖,從而獲得animationView NSLog(@"subviews count %ld", (unsigned long)[[[self view] subviews]count]); //1.獲取self.view的全部子視圖 NSArray *allSubViewsArray = [[self view] subviews]; //2.獲取灰色的視圖 UIView *grayView = (UIView *)[allSubViewsArray lastObject]; //3.改變灰色視圖的座標 [grayView setCenter:touchPoint]; } @end