当前位置:首页 > 操作系统 > Ios

iOS触摸事件

触摸常见的事件有以下几种,触摸事件一般写在view文件中,因为viewController文件有可能控制不止一个view,不适合写触摸事件

             1
            //
             开始触摸
             2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
 3     NSLog(@"开始触摸touch");
 4}
 5 6 7// 触摸结束 8 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
 9     NSLog(@"触摸结束touch");
10}
111213// 移动触摸14 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
15     NSLog(@"移动触摸touch");
1617// 1.获取触摸手势18     UITouch *touch = [touches anyObject];
1920// 2.获取触摸的位置(获取一个触摸在某个视图上的位置)21     CGPoint currentPoint = [touch locationInView:self.superview];
2223// 3.根据触摸的位置改变视图的位置24     self.center = currentPoint;
2526}
272829// 触摸被打断30 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
31     NSLog(@"触摸被打断touch");
32 }

 

原文:http://www.cnblogs.com/zhizunbao/p/5370906.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!