http://blog.sina.com.cn/s/blog_661314940100og3b.html
前面寫了幾次提到UITouch,也提到了UITouch的三個代理方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
這裏還有幾個代理方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//觸摸取消事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;//註冊接受觸摸事件的Control
下面寫了一個簡單的DEMO,觸摸時,移動圖片
- (void)loadView {
[super loadView];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 50.0, 45.0, 45.0)];
image.image = [UIImage imageNamed:@"1.png"];
image.tag = 100;
[self.view addSubview:image];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIImageView *view1 = (UIImageView*)[self.view viewWithTag:100];
CGPoint point = [touch locationInView:self.view];
CGRect frame = view1.frame;
frame.origin = point;
view1.frame = frame;
}