• 手势识别器基本练习


    Main.storyboard

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="vXZ-lx-hvc">
        <dependencies>
            <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
        </dependencies>
        <scenes>
            <!--View Controller-->
            <scene sceneID="ufC-wZ-h7g">
                <objects>
                    <viewController id="vXZ-lx-hvc" customClass="LWTViewController" sceneMemberID="viewController">
                        <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
                            <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <subviews>
                                <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="scene" id="i13-3w-YRs">
                                    <rect key="frame" x="48" y="149" width="224" height="182"/>
                                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                                </imageView>
                            </subviews>
                            <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                        </view>
                        <connections>
                            <outlet property="imageView" destination="i13-3w-YRs" id="omC-0A-gHm"/>
                        </connections>
                    </viewController>
                    <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
                </objects>
            </scene>
        </scenes>
        <resources>
            <image name="scene" width="223" height="181"/>
        </resources>
        <simulatedMetricsContainer key="defaultSimulatedMetrics">
            <simulatedStatusBarMetrics key="statusBar"/>
            <simulatedOrientationMetrics key="orientation"/>
            <simulatedScreenMetrics key="destination"/>
        </simulatedMetricsContainer>
    </document>
    View Code

    LWTViewController.m

    //
    //  LWTViewController.m
    //  手势识别器 -- UIGestureRecognizer
    //
    //  Created by apple on 14-6-14.
    //  Copyright (c) 2014年 lwt. All rights reserved.
    //
    
    #import "LWTViewController.h"
    
    @interface LWTViewController () <UIGestureRecognizerDelegate>
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation LWTViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        self.imageView.userInteractionEnabled = YES;
        self.imageView.multipleTouchEnabled = YES;
        
        // 拖拽
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
        
        [self.imageView addGestureRecognizer:pan];
    //    [self pinch];
    //    
    //    [self rotation];
    }
    
    - (void)panView: (UIPanGestureRecognizer *)pan
    {
        // NSLog(@"%@",pan);
        // 返回的值是以手指按下的点为原点
        CGPoint point = [pan translationInView:pan.view];
        
        // NSLog(@"拖拽 %@", NSStringFromCGPoint(point));
    
        self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
    //    CGPoint temp = self.imageView.center;
    //    temp.x += point.x;
    //    temp.y += point.y;
    //    self.imageView.center = temp;
        
        [pan setTranslation:CGPointZero inView:pan.view];
        
    }
    
    // 捏合
    - (void)pinch
    {
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
        pinch.delegate = self;
        
        [self.imageView addGestureRecognizer:pinch];
    }
    
    - (void)pinchView : (UIPinchGestureRecognizer *)pinch
    {
        // NSLog(@"%@",pinch);
        // self.imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
        
        self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
        
        pinch.scale = 1.0;
    }
    
    // 旋转
    - (void)rotation
    {
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
        
        rotation.delegate = self;
        
        [self.imageView addGestureRecognizer:rotation];
    }
    
    - (void)rotationView: (UIRotationGestureRecognizer *)rotation
    {
        // NSLog(@"%@",rotation);
        
        // 每次从最初的位置开始
        // self.iconView.transform = CGAffineTransformMakeRotation(gesture.rotation);
        // 在传入的transform基础上递增一个弧度
        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
        // 将旋转的弧度清零(注意不是将图片旋转的弧度清零, 而是将当前手指旋转的弧度清零)
        rotation.rotation = 0;
    }
    
    // 长按
    - (void)longPress
    {
        UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressView:)];
        
        longPress.minimumPressDuration = 2.0;
        // 手指按下后事件响应之前允许手指移动的偏移位
        longPress.allowableMovement = 50;
        
        [self.imageView addGestureRecognizer:longPress];
    }
    
    - (void)longPressView : (UILongPressGestureRecognizer *)longPress
    {
        NSLog(@"UILongPressGestureRecognizer");
    }
    
    // 轻扫
    - (void)swipe
    {
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
        
        // 不是所有的都能实现
        // swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionRight;
        // 设置轻扫的方向
        swipe.direction = UISwipeGestureRecognizerDirectionUp;
        
        [self.imageView addGestureRecognizer:swipe];
        
        UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
        swipe1.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.imageView addGestureRecognizer:swipe1];
        
        UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
        swipe2.direction = UISwipeGestureRecognizerDirectionDown;
        [self.imageView addGestureRecognizer:swipe2];
        
        UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
        swipe3.direction = UISwipeGestureRecognizerDirectionRight;
        [self.imageView addGestureRecognizer:swipe3];
    }
    
    - (void)swipeView : (UISwipeGestureRecognizer *)swipe
    {
        NSLog(@"UITapGestureRecognizer");
    }
    
    // 敲击
    - (void)tap
    {
        // 创建手势识别器
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        
        tap.delegate = self;
        
        // tap.numberOfTapsRequired = 2;
        // tap.numberOfTouchesRequired = 2;
        // 添加手势识别器到View
        [self.imageView addGestureRecognizer:tap];
        // 监听手势识别器
        [tap addTarget:self action:@selector(tapView:)];
    }
    
    - (void)tapView : (UITapGestureRecognizer *)tap
    {
        NSLog(@"UITapGestureRecognizer");
    }
    
    #pragma mark - UIGestureRecognizerDelegate
    /*
     // 该方法返回的BOOL值决定了view是否响应点击事件
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        CGPoint point = [touch locationInView:touch.view];
        if (point.x < self.imageView.bounds.size.width * 0.5) {
            return YES;
        }else
        {
            return NO;
        }
    }
     */
    
    // 该方法返回的BOOL值决定了view是否能够同时响应多个手势
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    @end
    View Code
  • 相关阅读:
    KVC
    MRC&ARC
    网络基础
    沙盒
    GCD深入了解
    iOS 架构模式MVVM
    iOS 源代码管理工具之SVN
    iOS给UIimage添加圆角的两种方式
    Objective-C 中,atomic原子性一定是安全的吗?
    iOS Block循环引用
  • 原文地址:https://www.cnblogs.com/wentianblog/p/3788537.html
Copyright © 2020-2023  润新知