UI初级连载十一-------触摸与手势
来源:互联网 发布:阿里云空间申请 编辑:程序博客网 时间:2024/05/22 17:43
//当子视图超出自己的frame时,是否剪切子视图
self.clipsToBounds= YES;
//是否开启多点触控
self.multipleTouchEnabled= YES;
点击方法:
//手指开始点击视图是调用的方法
- (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)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event;
//结束晃动时调用的方法
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event;
需要的对象:
//拿到一个touch对象
UITouch*touch = touches.anyObject;
//拿到点击的View
UIView*touchView = touch.view;
//拿到当前在view上的坐标
CGPointnowLocation = [touch locationInView:self];
//拿到上一次在view上的坐标
CGPointlastLocation = [touch previousLocationInView:self];
//拿到点击次数
NSIntegertapCount = touch.tapCount;
事件传递:
//分配事件所调用的方法
- (void)sendEvent:(UIEvent*)event;
响应者链:
响应者链表示一系列的响应者对象。时间被交由第一响应者对象处理,如果第一响应者不处理,事件被沿着响应者链向上传递,交给下一响应者(next responder)——— 具体参考UI09-05代码
手势识别器:
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *gestureView = [[UIViewalloc]initWithFrame:CGRectMake(50,50,300,300)];
gestureView.backgroundColor= [UIColorblackColor];
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *gestureView = [[UIViewalloc]initWithFrame:CGRectMake(50,50,300,300)];
gestureView.backgroundColor= [UIColorblackColor];
[self.viewaddSubview:gestureView];
/*------------------------点击手势-------------*/
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap1Action:)];
//注意:一个点击手势,只能识别一种手势,单击和双击是不同的两个手势
//设置点击的数量
tap1.numberOfTapsRequired= 1;
//设置点击的个数
tap1.numberOfTouchesRequired= 1;
//往gestureView上添加一个手势
[gestureView addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap2Action:)];
tap2.numberOfTapsRequired= 2;
//注意:一个点击手势,只能识别一种手势,单击和双击是不同的两个手势
//设置点击的数量
tap1.numberOfTapsRequired= 1;
//设置点击的个数
tap1.numberOfTouchesRequired= 1;
//往gestureView上添加一个手势
[gestureView addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap2Action:)];
tap2.numberOfTapsRequired= 2;
[gestureView addGestureRecognizer:tap2];
//如果参数中的手势出发了,则自身失效
[tap1 requireGestureRecognizerToFail:tap2];
/*---------------轻扫手势-------------*/
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
//设置方向---向上轻扫
swipeGesture.direction= UISwipeGestureRecognizerDirectionUp;
[gestureView addGestureRecognizer:swipeGesture];
/*---------------平移手势-------------*/
//平移手势(滑动)
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];
[gestureView addGestureRecognizer:panGesture];
/*---------------长按手势-------------*/
//长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];
//设置长安的最短时间
longPressGesture.minimumPressDuration= 3;
[gestureView addGestureRecognizer:longPressGesture];
/*---------------旋转手势-------------*/
//旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotation:)];
[gestureView addGestureRecognizer:rotationGesture];
/*----------捏合手势--------------*/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];
[gestureView addGestureRecognizer:pinch];
}
//单击
- (void)tap1Action:(UITapGestureRecognizer*)tap1
{
NSLog(@"单击");
- (void)tap1Action:(UITapGestureRecognizer*)tap1
{
NSLog(@"单击");
}
//双击
- (void)tap2Action:(UITapGestureRecognizer*)tap2
{
NSLog(@"双击");
}
//轻扫
- (void)swipe:(UISwipeGestureRecognizer*)swipe
{
if (swipe.direction== UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上清扫");
}
}
//平移
- (void)panAction:(UIPanGestureRecognizer*)pan
{
CGPoint p = [pan locationInView:pan.view];
NSLog(@"%@",NSStringFromCGPoint(p));
}
//长按
- (void)longPress:(UILongPressGestureRecognizer*)longPress
{
if (longPress.state== UIGestureRecognizerStateBegan) {
NSLog(@"开始长按");
}elseif (longPress.state== UIGestureRecognizerStateEnded) {
NSLog(@"结束长按");
}
}
//旋转
- (void)rotation:(UIRotationGestureRecognizer*)rotation
{
//获取旋转角度---弧度
CGFloat r = rotation.rotation;
//弧度转角度
//180/Pi = x/r
NSLog(@"r is %.2f",180 / M_PI* r);
- (void)tap2Action:(UITapGestureRecognizer*)tap2
{
NSLog(@"双击");
}
//轻扫
- (void)swipe:(UISwipeGestureRecognizer*)swipe
{
if (swipe.direction== UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上清扫");
}
}
//平移
- (void)panAction:(UIPanGestureRecognizer*)pan
{
CGPoint p = [pan locationInView:pan.view];
NSLog(@"%@",NSStringFromCGPoint(p));
}
//长按
- (void)longPress:(UILongPressGestureRecognizer*)longPress
{
if (longPress.state== UIGestureRecognizerStateBegan) {
NSLog(@"开始长按");
}elseif (longPress.state== UIGestureRecognizerStateEnded) {
NSLog(@"结束长按");
}
}
//旋转
- (void)rotation:(UIRotationGestureRecognizer*)rotation
{
//获取旋转角度---弧度
CGFloat r = rotation.rotation;
//弧度转角度
//180/Pi = x/r
NSLog(@"r is %.2f",180 / M_PI* r);
}
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer*)pinch
{
//获得捏合的比例
CGFloat scale = pinch.scale;
// pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale);
pinch.view.transform= CGAffineTransformMakeScale(scale, scale);
}
ViewController.h文件:
#import<UIKit/UIKit.h>
#import "MyView.h"
@interfaceViewController : UIViewController<MyPotocol>
@property(weak,nonatomic)IBOutletMyView*myView;
@property(weak,nonatomic)IBOutletUILabel*label;
#import "MyView.h"
@interfaceViewController : UIViewController<MyPotocol>
@property(weak,nonatomic)IBOutletMyView*myView;
@property(weak,nonatomic)IBOutletUILabel*label;
@end
ViewController.m文件:
#import"ViewController.h"
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myView.delegate= self;
}
//单击、双击
- (void)touchTap:(NSString*)str
{
self.label.text= str;
}
- (void)distance:(CGFloat)dis
{
if (dis < 0) {
self.label.text= @"向左轻扫";
}elseif(dis >0) {
self.label.text= @"向右轻扫";
}
}
//捏合
- (void)change:(CGFloat)distance
{
if (distance > 0) {
self.label.text= @"捏合放大";
}elseif (distance < 0){
self.label.text= @"捏合缩小";
}else{
self.label.text= @"平移";
}
}
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myView.delegate= self;
}
//单击、双击
- (void)touchTap:(NSString*)str
{
self.label.text= str;
}
- (void)distance:(CGFloat)dis
{
if (dis < 0) {
self.label.text= @"向左轻扫";
}elseif(dis >0) {
self.label.text= @"向右轻扫";
}
}
//捏合
- (void)change:(CGFloat)distance
{
if (distance > 0) {
self.label.text= @"捏合放大";
}elseif (distance < 0){
self.label.text= @"捏合缩小";
}else{
self.label.text= @"平移";
}
}
@end
UI09-homework:使用touch的几种方法在label上打印出一只手单击,一只手双击,两只手单击,两只手双击,向左轻扫,向右轻扫
MyView.h文件:
#import<UIKit/UIKit.h>
@protocolMyPotocol <NSObject>
//单击、双击
- (void)touchTap:(NSString*)str;
//捏合
- (void)change:(CGFloat)distance;
//轻扫
- (void)touchTap:(NSString*)str;
//捏合
- (void)change:(CGFloat)distance;
//轻扫
- (void)distance:(CGFloat)dis;
@end
@interfaceMyView : UIView
@property(nonatomic,assign)id<MyPotocol> delegate;
@end
MyView.m文件:
#import"MyView.h"
@implementationMyView
- (void)awakeFromNib
{
[superawakeFromNib];
@implementationMyView
- (void)awakeFromNib
{
[superawakeFromNib];
self.multipleTouchEnabled= YES;
}
//单击、双击
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//拿到手指的个数
NSInteger fingures = touches.count;
//拿到某一对象
UITouch *touch = touches.anyObject;
//对象的点击次数
NSInteger count = touch.tapCount;
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//拿到手指的个数
NSInteger fingures = touches.count;
//拿到某一对象
UITouch *touch = touches.anyObject;
//对象的点击次数
NSInteger count = touch.tapCount;
[selftapTouch:fingurestapCount:count];
}
//单击、双击方法
- (void)tapTouch:(NSInteger)touches tapCount:(NSInteger)count
{
if (touches < 2) {
if (count < 2) {
[selfperformSelector:@selector(singleTap1)withObject:nilafterDelay:0.5];
}else{
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTap1)object:nil];
[selfdoubleTap1];
}
}elseif (touches ==2){
if (count < 2) {
[selfperformSelector:@selector(singleTap2)withObject:nilafterDelay:0.5];
}
else{
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTap2)object:nil];
[selfdoubleTap2];
}
}
}
- (void)singleTap1
{
NSString *str = @"一只手单击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
- (void)doubleTap1
{
NSString *str = @"一只手双击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
- (void)singleTap2
{
NSString *str = @"两只手单击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
- (void)doubleTap2
{
NSString *str = @"两只手双击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
//捏合放大,捏合缩小,向左向右清扫
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
//拿到手指的个数
NSInteger fingures = touches.count;
if (fingures == 1) {
//拿到某一对象
UITouch *touch = touches.anyObject;
CGPoint nowPoint = [touchlocationInView:self];
CGPoint prePoint = [touchpreviousLocationInView:self];
CGFloat distance = nowPoint.x- prePoint.x;
if ([self.delegaterespondsToSelector:@selector(distance:)]) {
[self.delegatedistance:distance];
}
}elseif (fingures == 2) {
NSArray *allObjects = touches.allObjects;
UITouch *touch1 = allObjects[0];
UITouch *touch2 = allObjects[1];
CGPoint nowPoint1 = [touch1locationInView:self];
CGPoint prePoint1 = [touch1previousLocationInView:self];
CGPoint nowPoint2 = [touch2locationInView:self];
CGPoint prePoint2 = [touch2previousLocationInView:self];
//计算两点间的距离
CGFloat nowDistance = [selfdistanceWithFirstPosition:nowPoint1SecondPoint:nowPoint2];
CGFloat PreDistence = [selfdistanceWithFirstPosition:prePoint1SecondPoint:prePoint2];
CGFloat sub = nowDistance - PreDistence;
if ([self.delegaterespondsToSelector:@selector(change:)]) {
//单击、双击方法
- (void)tapTouch:(NSInteger)touches tapCount:(NSInteger)count
{
if (touches < 2) {
if (count < 2) {
[selfperformSelector:@selector(singleTap1)withObject:nilafterDelay:0.5];
}else{
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTap1)object:nil];
[selfdoubleTap1];
}
}elseif (touches ==2){
if (count < 2) {
[selfperformSelector:@selector(singleTap2)withObject:nilafterDelay:0.5];
}
else{
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTap2)object:nil];
[selfdoubleTap2];
}
}
}
- (void)singleTap1
{
NSString *str = @"一只手单击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
- (void)doubleTap1
{
NSString *str = @"一只手双击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
- (void)singleTap2
{
NSString *str = @"两只手单击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
- (void)doubleTap2
{
NSString *str = @"两只手双击";
if ([self.delegaterespondsToSelector:@selector(touchTap:)]) {
[self.delegatetouchTap:str];
}
}
//捏合放大,捏合缩小,向左向右清扫
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
//拿到手指的个数
NSInteger fingures = touches.count;
if (fingures == 1) {
//拿到某一对象
UITouch *touch = touches.anyObject;
CGPoint nowPoint = [touchlocationInView:self];
CGPoint prePoint = [touchpreviousLocationInView:self];
CGFloat distance = nowPoint.x- prePoint.x;
if ([self.delegaterespondsToSelector:@selector(distance:)]) {
[self.delegatedistance:distance];
}
}elseif (fingures == 2) {
NSArray *allObjects = touches.allObjects;
UITouch *touch1 = allObjects[0];
UITouch *touch2 = allObjects[1];
CGPoint nowPoint1 = [touch1locationInView:self];
CGPoint prePoint1 = [touch1previousLocationInView:self];
CGPoint nowPoint2 = [touch2locationInView:self];
CGPoint prePoint2 = [touch2previousLocationInView:self];
//计算两点间的距离
CGFloat nowDistance = [selfdistanceWithFirstPosition:nowPoint1SecondPoint:nowPoint2];
CGFloat PreDistence = [selfdistanceWithFirstPosition:prePoint1SecondPoint:prePoint2];
CGFloat sub = nowDistance - PreDistence;
if ([self.delegaterespondsToSelector:@selector(change:)]) {
[self.delegatechange:sub];
}
}
}
//计算两点间的距离
- (CGFloat)distanceWithFirstPosition:(CGPoint) firstP SecondPoint:(CGPoint) secondP
{
CGFloat distance = powf(firstP.x- secondP.x,2) +powf(firstP.y- secondP.y,2);
return sqrtf(distance);
//计算两点间的距离
- (CGFloat)distanceWithFirstPosition:(CGPoint) firstP SecondPoint:(CGPoint) secondP
{
CGFloat distance = powf(firstP.x- secondP.x,2) +powf(firstP.y- secondP.y,2);
return sqrtf(distance);
}
@end
0 0
- UI初级连载十一-------触摸与手势
- UI初级第九课 手势与触摸——iOS学习连载24
- IOS UI初级-触摸与手势的基本概念和用法
- UI高级--触摸与手势
- iOS开发 UI 触摸手势
- touch手势与触摸
- 触摸与手势
- 触摸与手势原理
- 触摸与手势
- IOS触摸与手势
- 触摸与手势-----事件
- 触摸与手势事件
- 【UI初级 连载四】--------UI常用控件
- UI之摇晃手机和触摸手势
- UI触摸手势,事件的分发传递
- UI初级连载八------------标签控制器
- UI初级连载九----------UITableView的使用
- 【UI初级 连载一】------iPhone开发入门
- 第三周,顺序表的基本运算(3)
- 第三周——项目1 顺序表的基本运算
- Handler消息传递机制
- 近期小结
- 第三周项目1-顺序表的基本运算
- UI初级连载十一-------触摸与手势
- 足清霜根治脚气
- 自定义alertView
- VFW连接视频驱动不成功问题解决
- 好累
- 第2周 项目3-体验复杂度(1)
- 【第3周 项目3 - 求集合并集】
- 第三周项目一
- 第2周项目3 体验复杂度