触摸事件在iOS中是最常用的事件,这里我们来介绍下触摸事件。
在下面的例子中定义UIImageView。首先我们在TouchEventViewController中添加触摸事件,并利用触摸移动事件来移动Image,具体代码如下:
@implementation TouchEvenViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView * _image=[[KCImage alloc]initWithFrame:CGRectMake(50, 50, 150, 169)];
[_image setImage:[UIImage imageName:@"zmit"]];
[self.view addSubview:_image];
}
#pragma mark - 视图控制器的触摸事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"开始触摸");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//取得一个触摸对象(对于多点触摸可能有多个对象)
UITouch *touch=[touches anyObject];
//取得当前位置
CGPoint current=[touch locationInView:self.view];
//取得前一个位置
CGPoint previous=[touch previousLocationInView:self.view];
//移动前的中点位置
CGPoint center=_image.center;
//移动偏移量
CGPoint offset=CGPointMake(current.x-previous.x, current.y-previous.y);
//重新设置新位置
_image.center=CGPointMake(center.x+offset.x, center.y+offset.y);
NSLog(@触摸中");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸结束");
}
@end
一个简单的拖动的触摸事件,在这里展示给大家了。
韩剧再婚皇后阿里云盘全集高清1080P资源链接-韩剧再婚皇后免费阿里云盘资源下载4K高清无删减版链接
poipiku网站如何打开-poipiku官网网页版访问入口
明日方舟:终末地-云游戏畅玩入口-明日方舟:终末地-网页版在线直连地址
poki在线小游戏网页版-poki免费游戏大全入口
poki游戏入口-点开即玩-poki游戏-在线免费畅玩
原神akt大佬资源viciNeko入口链接-原神akt大佬资源网页vicineko入口链接分享