首页 > 代码库 > iOS_38_手势

iOS_38_手势

Pan平移手势
最终效果图:

//
//  PanController.m
//  38_手势
//
//  Created by beyond on 14-9-16.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "PanController.h"

@interface PanController ()
// nana头像所在的View
@property (weak, nonatomic) IBOutlet UIView *nanaView;
- (IBAction)dismiss;

@end

@implementation PanController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 创建pan手势,并绑定监听方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [self.nanaView addGestureRecognizer:pan];
}

- (void)panView:(UIPanGestureRecognizer *)pan
{
    
    switch (pan.state) {
        case UIGestureRecognizerStateBegan: // 开始触发手势
            
            break;
            
        case UIGestureRecognizerStateEnded: // 手势结束
            
            break;
            
        default:
            break;
    }
    
    // 1.在view上面挪动的距离
    CGPoint translation = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x += translation.x;
    center.y += translation.y;
    pan.view.center = center;
    
    // 2.清空移动的距离
    [pan setTranslation:CGPointZero inView:pan.view];
}

#pragma mark - 连线
- (IBAction)dismiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end


Tap手势
//
//  TapController.m
//  38_手势
//
//  Created by beyond on 14-9-16.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "TapController.h"

// 手势代理
@interface TapController ()<UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *nanaImgView;

- (IBAction)dismiss;

@end

@implementation TapController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _nanaImgView.userInteractionEnabled = YES;
    _nanaImgView.multipleTouchEnabled = YES;
    //[self testTap];
    [self testTap2];
}

- (void)testTap
{
    // 1.创建Tap手势识别器对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    // 两根手势,连续敲击2次,手势才能识别成功
    tap.numberOfTapsRequired = 2;
    tap.numberOfTouchesRequired = 2;
    
    // 2.添加监听方法(识别到了对应的手势,就会调用监听方法)
    [tap addTarget:self action:@selector(taping)];
    
    // 3.为nanaImgView 添加Tap手势识别器对象
    [self.nanaImgView addGestureRecognizer:tap];
}

- (void)testTap2
{
    // 1.创建Tap手势识别器对象,同时绑定监听方法(识别到了对应的手势,就会调用监听方法)
    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taping)];
    // 2.设置手势的代理,目的是:决定手势只有在特定场合才会被识别(触发监听的方法)
    tap.delegate = self;
    // 3.为nanaImgView 添加Tap手势识别器对象
    [self.nanaImgView addGestureRecognizer:tap];
}
#define kRandomColor [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0]
// 监听的方法
- (void)taping
{
    // 每次tap,随机变换背景颜色
    self.view.backgroundColor = kRandomColor;
    NSLog(@"-----taping");
}
#pragma mark - gestureRecognizer的代理方法
//  当点击view的时候,会先询问这个方法,是否接收本次tap点击(即是否为有效tap)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint pos = [touch locationInView:touch.view];
    // 点击图片的左半边有效,右半边无效
    if (pos.x <= self.nanaImgView.frame.size.width * 0.5) {
        return YES;
    }
    return NO;
}




#pragma mark - 连线方法
- (IBAction)dismiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

Swipe轻扫手势


LongPress长按手势




长按手势的主要属性参数



//
//  SwipeLongPressController.m
//  38_手势
//
//  Created by beyond on 14-9-17.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "SwipeLongPressController.h"

@interface SwipeLongPressController ()
@property (weak, nonatomic) IBOutlet UIImageView *nanaImgView;
- (IBAction)dismiss;

@end

@implementation SwipeLongPressController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.允许交互
    _nanaImgView.userInteractionEnabled = YES;
    
    // 2.自定义方法,添加swipe手势
    [self addSwipe];
    
    // 3.自定义方法,添加longPress手势
    [self addLongPress];
}

// 2.自定义方法,添加swipe手势
- (void)addSwipe
{
    // 1.创建Swipe手势识别器对象,同时绑定监听方法(识别到了对应的手势,就会调用监听方法)
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiping)];
    // 设置属性:轻扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    // 2.为nanaImgView 添加Swipe手势识别器对象
    [self.nanaImgView addGestureRecognizer:swipe];
}
// 3.自定义方法,添加longPress手势
- (void)addLongPress
{
    // 1.创建LongPress手势识别器对象,同时绑定监听方法(识别到了对应的手势,就会调用监听方法)
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
    [longPress addTarget:self action:@selector(longPressing)];
    
    // 设置属性:至少长按2秒,默认0.5秒
    longPress.minimumPressDuration = 2;
    // 按下之后,不松手,在能触发手势之前,可允许移动的范围,50px范围内长按有效,默认是10px
    longPress.allowableMovement = 50;
    
    // 2.为nanaImgView 添加Swipe手势识别器对象
    [self.nanaImgView addGestureRecognizer:longPress];
}
#define kRandomColor [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0]
// 手势的监听的方法
- (void)swiping
{
    self.view.backgroundColor = kRandomColor;
    NSLog(@"-----swiping");
}
// 手势的监听的方法
- (void)longPressing
{
    self.view.backgroundColor = kRandomColor;
    NSLog(@"-------长按了nanaImgView");
}



- (IBAction)dismiss
{
    [self dismissViewControllerAnimated:YES completion:nil   ];
}
@end




































































iOS_38_手势