uitouch用法

本文介绍如何使用Objective-C实现触摸事件处理,通过触摸开始、移动和结束触发图片位置变化,提供了一个直观的触摸交互体验。文章通过实例演示了如何在视图中响应触摸事件,动态调整图片位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值