UI初级连载十一-------触摸与手势

// 当子视图超出自己的 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 上的坐标
CGPoint nowLocation = [touch locationInView:self];
// 拿到上一次在 view 上的坐标
CGPoint lastLocation = [touch previousLocationInView : self ];
// 拿到点击次数
NSInteger tapCount = touch.tapCount;



事件传递:
// 分配事件所调用的方法
- (void)sendEvent:(UIEvent *)event;

响应者链:
响应者链表示一系列的响应者对象。时间被交由第一响应者对象处理,如果第一响应者不处理,事件被沿着响应者链向上传递,交给下一响应者(next responder)——— 具体参考UI09-05代码


手势识别器:
- ( void )viewDidLoad {
    [
super viewDidLoad ];
   
// Do any additional setup after loading the view, typically from a nib.
   
   
UIView *gestureView = [[ UIView alloc ] initWithFrame : CGRectMake ( 50 , 50 , 300 , 300 )];
    gestureView.
backgroundColor = [ UIColor blackColor ];
    [self.view addSubview:gestureView];
    /*------------------------ 点击手势 -------------*/
    UITapGestureRecognizer *tap1 = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (tap1Action:)];
   
// 注意:一个点击手势,只能识别一种手势,单击和双击是不同的两个手势
   
// 设置点击的数量
    tap1.
numberOfTapsRequired = 1 ;
   
// 设置点击的个数
    tap1.
numberOfTouchesRequired = 1 ;
   
// gestureView 上添加一个手势
    [gestureView
addGestureRecognizer :tap1];
   
   
UITapGestureRecognizer *tap2 = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (tap2Action:)];
    tap2.
numberOfTapsRequired = 2 ;
    [gestureView addGestureRecognizer:tap2];
    // 如果参数中的手势出发了,则自身失效
    [tap1 requireGestureRecognizerToFail:tap2];
   
   
/*--------------- 轻扫手势 -------------*/
   
UISwipeGestureRecognizer *swipeGesture = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (swipe:)];
   
// 设置方向---向上轻扫
    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
    [gestureView addGestureRecognizer:swipeGesture];
   
   
/*--------------- 平移手势 -------------*/
   
// 平移手势(滑动)
   
UIPanGestureRecognizer *panGesture = [[ UIPanGestureRecognizer alloc ] initWithTarget : self action : @selector (panAction:)];
    [gestureView addGestureRecognizer:panGesture];
   
   
/*--------------- 长按手势 -------------*/
   
// 长按手势
   
UILongPressGestureRecognizer *longPressGesture = [[ UILongPressGestureRecognizer alloc ] initWithTarget : self action : @selector (longPress:)];
   
// 设置长安的最短时间
    longPressGesture.
minimumPressDuration = 3 ;
    [gestureView
addGestureRecognizer :longPressGesture];
   
   
/*--------------- 旋转手势 -------------*/
   
// 旋转手势
   
UIRotationGestureRecognizer *rotationGesture = [[ UIRotationGestureRecognizer alloc ] initWithTarget : self action : @selector (rotation:)];
    [gestureView
addGestureRecognizer :rotationGesture];
   
   
/*---------- 捏合手势 --------------*/
   
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (pinchAction:)];
    [gestureView
addGestureRecognizer :pinch];
}
// 单击
- (
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 ( @" 开始长按 " );
    }
else if (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"

@interface ViewController : UIViewController < MyPotocol >

@property ( weak , nonatomic ) IBOutlet MyView *myView;
@property ( weak , nonatomic ) IBOutlet UILabel *label;

@end
ViewController.m文件:
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
// 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 = @" 向左轻扫 " ;
    }
else if (dis > 0 ) {
       
self . label . text = @" 向右轻扫 " ;

    }
}

// 捏合
- (
void )change:( CGFloat )distance
{
   
if (distance > 0 ) {
       
self . label . text = @" 捏合放大 " ;
    }
else if (distance < 0 ){
       
self . label . text = @" 捏合缩小 " ;
       
    }
else {
       
self . label . text = @" 平移 " ;
    }
}

@end

UI09-homework:使用touch的几种方法在label上打印出一只手单击,一只手双击,两只手单击,两只手双击,向左轻扫,向右轻扫
MyView.h文件:
#import <UIKit/UIKit.h>

@protocol MyPotocol < NSObject >
// 单击、双击
- (
void )touchTap:( NSString *)str;
// 捏合
- (
void )change:( CGFloat )distance;
// 轻扫
- (void)distance:(CGFloat)dis;
@end
@interface MyView : UIView
@property ( nonatomic , assign ) id < MyPotocol > delegate;
@end
MyView.m文件:
#import "MyView.h"

@implementation MyView
- (
void )awakeFromNib
{
    [
super awakeFromNib ];
    self.multipleTouchEnabled = YES;
}
// 单击、双击
- (
void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event
{
   
// 拿到手指的个数
   
NSInteger fingures = touches. count ;
   
// 拿到某一对象
   
UITouch *touch = touches. anyObject ;
   
// 对象的点击次数
   
NSInteger count = touch. tapCount ;
    [self tapTouch:fingures tapCount:count];
}
// 单击、双击方法
- (
void )tapTouch:( NSInteger )touches tapCount:( NSInteger )count
{
   
if (touches < 2 ) {
       
if (count < 2 ) {
            [
self performSelector : @selector (singleTap1) withObject : nil afterDelay : 0.5 ];
           
        }
else {
            [
NSObject cancelPreviousPerformRequestsWithTarget : self selector : @selector (singleTap1) object : nil ];
            [
self doubleTap1 ];
        }
    }
else if (touches == 2 ){
       
if (count < 2 ) {
            [
self performSelector : @selector (singleTap2) withObject : nil afterDelay : 0.5 ];
        }
       
else {
            [
NSObject cancelPreviousPerformRequestsWithTarget : self selector : @selector (singleTap2) object : nil ];
            [
self doubleTap2 ];
        }
    }
}

- (
void )singleTap1
{
   
NSString *str = @" 一只手单击 " ;
   
if ([ self . delegate respondsToSelector : @selector (touchTap:)]) {
        [
self . delegate touchTap :str];
    }
}
- (
void )doubleTap1
{
   
NSString *str = @" 一只手双击 " ;
   
if ([ self . delegate respondsToSelector : @selector (touchTap:)]) {
        [
self . delegate touchTap :str];
    }
}
- (
void )singleTap2
{
   
NSString *str = @" 两只手单击 " ;
   
if ([ self . delegate respondsToSelector : @selector (touchTap:)]) {
        [
self . delegate touchTap :str];
    }
   
}
- (
void )doubleTap2
{
   
NSString *str = @" 两只手双击 " ;
   
if ([ self . delegate respondsToSelector : @selector (touchTap:)]) {
        [
self . delegate touchTap :str];
    }
}

// 捏合放大 , 捏合缩小,向左向右清扫
- (
void )touchesMoved:( NSSet *)touches withEvent:( UIEvent *)event
{
   
// 拿到手指的个数
   
NSInteger fingures = touches. count ;
   
if (fingures == 1 ) {
       
// 拿到某一对象
       
UITouch *touch = touches. anyObject ;
       
CGPoint nowPoint = [touch locationInView : self ];
       
CGPoint prePoint = [touch previousLocationInView : self ];
       
CGFloat distance = nowPoint. x - prePoint. x ;
       
if ([ self . delegate respondsToSelector : @selector (distance:)]) {
            [
self . delegate distance :distance];
        }
    }
else if (fingures == 2 ) {
       
       
NSArray *allObjects = touches. allObjects ;
       

       
UITouch *touch1 = allObjects[ 0 ];
       
UITouch *touch2 = allObjects[ 1 ];
       
       
CGPoint nowPoint1 = [touch1 locationInView : self ];
       
CGPoint prePoint1 = [touch1 previousLocationInView : self ];
       
       
CGPoint nowPoint2 = [touch2 locationInView : self ];
       
CGPoint prePoint2 = [touch2 previousLocationInView : self ];
       
       
// 计算两点间的距离
       
CGFloat nowDistance = [ self distanceWithFirstPosition :nowPoint1 SecondPoint :nowPoint2];
       
CGFloat PreDistence = [ self distanceWithFirstPosition :prePoint1 SecondPoint :prePoint2];
       
       
CGFloat sub = nowDistance - PreDistence;
       
       
if ([ self . delegate respondsToSelector : @selector (change:)]) {
            [self.delegate change: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);
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值