IOS学习 触摸和手势UITouch 单击双击、移动视图
2016-03-14 16:56
441 查看
@interface HomeViewController :
UIViewController
{
UIView *touchView;
UITouch *touch;
NSInteger *tapcount;
CGPoint point;
UIView *movieView;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
touchView = [[UIView
alloc]initWithFrame:CGRectMake(0,
20,
self.view.bounds.size.width,
500)];
touchView.backgroundColor = [UIColor
cyanColor];
[self.view
addSubview:touchView];
//是否支持多点触摸,默认为NO
touchView.multipleTouchEnabled =
YES ;
//是否开启触摸事件,默认为开启
touchView.userInteractionEnabled =
YES;
movieView = [[UIView
alloc]initWithFrame:CGRectMake(0,
0, 50,
50)];
movieView.backgroundColor = [UIColor
redColor];
[touchView
addSubview:movieView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesBegan");
touch = [touches
anyObject]; //获取点击内容
tapcount =
touch.tapCount;
//获取短时间内点击的次数
NSLog(@"tapCount = %d",tapcount);
//设定单双击事件
if (tapcount ==
1) {
//延迟时间调用单击事件
[self
performSelector:@selector(singleTap)
withObject:nil
afterDelay:0.5];
}else if(tapcount ==
2){
//取消单击延迟事件
[NSObject
cancelPreviousPerformRequestsWithTarget:self
selector:@selector(singleTap)
object:nil];
[self doubleTap];
}
//获取触摸点在视图上的坐标
point = [touch
locationInView:touchView];
NSLog(@"%@",NSStringFromCGPoint(point));//将坐标转换成字符打印
}
- (void)singleTap{
NSLog(@"单击");
}
- (void)doubleTap{
NSLog(@"双击");
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesMoved");
//重新获取
touch = [touches
anyObject];
point = [touch
locationInView:touchView];
CGRect frame =
movieView.frame;
//获取frame值
frame.origin =
point;
//将移动光标的坐标赋值给frame
movieView.frame = frame;
//重新赋值给视图movieView
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesEnded");
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesCancelled");
}
UIViewController
{
UIView *touchView;
UITouch *touch;
NSInteger *tapcount;
CGPoint point;
UIView *movieView;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
touchView = [[UIView
alloc]initWithFrame:CGRectMake(0,
20,
self.view.bounds.size.width,
500)];
touchView.backgroundColor = [UIColor
cyanColor];
[self.view
addSubview:touchView];
//是否支持多点触摸,默认为NO
touchView.multipleTouchEnabled =
YES ;
//是否开启触摸事件,默认为开启
touchView.userInteractionEnabled =
YES;
movieView = [[UIView
alloc]initWithFrame:CGRectMake(0,
0, 50,
50)];
movieView.backgroundColor = [UIColor
redColor];
[touchView
addSubview:movieView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesBegan");
touch = [touches
anyObject]; //获取点击内容
tapcount =
touch.tapCount;
//获取短时间内点击的次数
NSLog(@"tapCount = %d",tapcount);
//设定单双击事件
if (tapcount ==
1) {
//延迟时间调用单击事件
[self
performSelector:@selector(singleTap)
withObject:nil
afterDelay:0.5];
}else if(tapcount ==
2){
//取消单击延迟事件
[NSObject
cancelPreviousPerformRequestsWithTarget:self
selector:@selector(singleTap)
object:nil];
[self doubleTap];
}
//获取触摸点在视图上的坐标
point = [touch
locationInView:touchView];
NSLog(@"%@",NSStringFromCGPoint(point));//将坐标转换成字符打印
}
- (void)singleTap{
NSLog(@"单击");
}
- (void)doubleTap{
NSLog(@"双击");
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesMoved");
//重新获取
touch = [touches
anyObject];
point = [touch
locationInView:touchView];
CGRect frame =
movieView.frame;
//获取frame值
frame.origin =
point;
//将移动光标的坐标赋值给frame
movieView.frame = frame;
//重新赋值给视图movieView
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesEnded");
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesCancelled");
}
相关文章推荐
- Android进阶UI之SwipeListView左滑删除例子
- Android进阶UI之SlidingTabLayout实现ViewPager页卡滑动效果
- Android bluetooth介绍(一):基本概念及硬件接口
- 使用CSS3 Media Queries实现网页自适应
- QtQuick基础教程(二)---QML基本语法
- Day3.2--Android简单UI控件之TextView的高级使用--SpannableString的使用
- 有关UIViewController与UIView的初始化创建
- Android 中 onTouch 和OnClick 冲突的处理(onTouchEvent返回true时与onclick冲突)
- CodeForces 622A Infinite Sequence
- easyui textBox控件,监听事件不好使的解决办法
- Knowing When to Use Override and New Keywords (C# Programming Guide)
- 关于IE8下media query兼容的解决方案探讨
- leetcode 232. Implement Queue using Stacks
- iOS开发64-UICollectionView设置间距的技巧
- ActionScript,Flash,Flash/Flex Builder,FlashPlayer,AIR,swf,swc,swz之间的区别
- UIKit性能调优实战讲解
- quick(3.2) UIListView扩展
- EasyUI常用控件禁用方法:
- 设置UIImage的渲染模式:UIImage.renderingMode
- leetcode 62. Unique Paths