iOS基礎(chǔ)之交互處理(UIButton、手勢(shì))

目錄
  1. UIButton: UIControl       按鈕
  2. 手勢(shì)

前言

注意:
    UIImageView 和 UILabel 是少數(shù)幾個(gè)不可交互的控件,需要設(shè)置userInteractionEnabled為true,否則不允許交互。

1. UIButton 按鈕(: UIControl)

// 創(chuàng)建
UIButton *button=[UIButton new];
[self.view addSubview:button];
[button autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];

常用

    /*
     UIControlStateNormal           普通狀態(tài)
     UIControlStateHighlighted      觸摸狀態(tài)(高亮)
     UIControlStateDisabled         禁用狀態(tài)
     UIControlStateSelected         選中狀態(tài)
     UIControlStateFocused
     UIControlStateApplication
     UIControlStateReserved
     */
    // 設(shè)置 標(biāo)題
    [button setTitle:@"" forState:UIControlStateNormal];
    // 設(shè)置 標(biāo)題顏色
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    // 設(shè)置 字體
    [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
    // 設(shè)置 標(biāo)題陰影色
    [button setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];

    // 設(shè)置 圖片
    // imageView的大小和UIbutton的大小是不一致的,iphoneX機(jī)型下很明顯可以看到。
    // 設(shè)置setContentVerticalAlignment、setContentHorizontalAlignment設(shè)置為fill可使大小一致 
    [button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    // 設(shè)置 圖片顯示模式
    [button.imageView setContentMode:UIViewContentModeScaleToFill];

    // 設(shè)置 背景圖片
    // 圖片大小會(huì)和按鈕大小保持一致
    [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    // 設(shè)置 背景顏色
    [button setBackgroundColor:[UIColor redColor]];
    // 設(shè)置 觸摸時(shí)圖片/背景圖片是否變暗
    [button setAdjustsImageWhenHighlighted:true];

    // 設(shè)置 標(biāo)題偏移
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    // 設(shè)置 圖片偏移
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    // 設(shè)置 內(nèi)容偏移
    [button setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    // 設(shè)置 內(nèi)容縱向、水平對(duì)齊方式
    [button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
    [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
/*
    UIControlContentHorizontalAlignmentCenter = 0,
    UIControlContentHorizontalAlignmentLeft   = 1,
    UIControlContentHorizontalAlignmentRight  = 2,
    UIControlContentHorizontalAlignmentFill   = 3,
*/
/*
    UIControlContentVerticalAlignmentCenter        = 0,  // 默認(rèn)內(nèi)容是居中的
    UIControlContentVerticalAlignmentTop           = 1,
    UIControlContentVerticalAlignmentBottom        = 2,
    UIControlContentVerticalAlignmentFill          = 3,   // 填充
*/

    // 設(shè)置 是否選中(true則進(jìn)入U(xiǎn)IControlStateSelected狀態(tài))
    [button setSelected:true];
    // 設(shè)置 是否允許用戶交互
    [button setUserInteractionEnabled:true];
    // 添加 點(diǎn)擊事件
    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    //-------------- 其他 --------------
    // 設(shè)置 不可用時(shí)是否變暗
    [button setAdjustsImageWhenDisabled:true];
    // 設(shè)置 觸摸時(shí)是否高亮
    [button setShowsTouchWhenHighlighted:true];

    // 獲取 img、bgImg、titleColor、titleShadowColor、title、AttributedTitle
    UIImage *img=button.currentImage;
    UIImage *bgImg=button.currentBackgroundImage;
    UIColor *titleColor=button.currentTitleColor;
    UIColor *shadowTitleColor=button.currentTitleShadowColor;
    NSString *title=button.currentTitle;
    NSAttributedString *titleStr=button.currentAttributedTitle;
繼承自UIView 

    // 設(shè)置 alpha透明度
    [button setAlpha:0.5];
    // 設(shè)置 是否隱藏
    [button setHidden:true];
    // 設(shè)置 填充色
    [button setTintColor:[UIColor blueColor]];
    // 設(shè)置是否可用
    [button setEnabled:true];
    // 設(shè)置 邊框顏色、邊框?qū)挾?、邊框圓角、是否裁剪
    [button.layer setBorderColor:[UIColor redColor].CGColor];
    [button.layer setBorderWidth:1.0];
    [button.layer setCornerRadius:5.0];
    [button.layer setMasksToBounds:true];
系統(tǒng)自帶UIButton類型

    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    /*
     UIButtonTypeCustom = 0,            前面不帶圖標(biāo),              默認(rèn)文字顏色為白色,無觸摸時(shí)高亮
     UIButtonTypeSystem                 前面不帶圖標(biāo),          默認(rèn)文字顏色為藍(lán)色,有觸摸時(shí)高亮
     UIButtonTypeDetailDisclosure,      前面帶“!”圖標(biāo)按鈕  ,默認(rèn)文字顏色為藍(lán)色,有觸摸時(shí)高亮
     UIButtonTypeInfoLight,             為感嘆號(hào)“!”圓形按鈕
     UIButtonTypeInfoDark,              為感嘆號(hào)“!”圓形按鈕
     UIButtonTypeContactAdd,            前面帶“+”圖標(biāo)按鈕,默認(rèn)文字顏色為藍(lán)色,有觸摸時(shí)高
     UIButtonTypePlain
     UIButtonTypeRoundedRect
     */

SDWebImage

#import <SDWebImage/UIButton+WebCache.h>

    [button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
    [button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
    [button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
    [button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    }];
    [button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    }];
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    }];
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    }];
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    }];
  1. 避免短時(shí)間多次點(diǎn)擊按鈕

》》》 》》》 1. 創(chuàng)建分類文件

UIButton+FQTimeButton.h

#import <UIKit/UIKit.h>

#define defaultInterval 2.0// 默認(rèn)間隔時(shí)間

@interface UIButton (FQTimeButton)
// 設(shè)置點(diǎn)擊時(shí)間間隔
@property (nonatomic, assign) NSTimeInterval timeInterVal;
@end

UIButton+FQTimeButton.m

#import "UIButton+FQTimeButton.h"

@interface UIButton()
// 用來判斷 是否去執(zhí)行方法
@property (nonatomic, assign) BOOL isExcuteEvent;
@end


@implementation UIButton (FQTimeButton)

// main方法之前會(huì)調(diào)用d所有類的load方法
+ (void)load{
    // 僅執(zhí)行一次,避免多次替換出錯(cuò)
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        // 交換自定義方法和系統(tǒng)方法
        SEL oldSel = @selector(sendAction:to:forEvent:);
        SEL newSel = @selector(newSendAction:to:forEvent:);
        Method oldMethod = class_getInstanceMethod(self, oldSel);
        Method newMethod = class_getInstanceMethod(self, newSel);
        BOOL isAdd = class_addMethod(self, oldSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
        if (isAdd) {
            class_replaceMethod(self, newSel, method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
        }else{
            method_exchangeImplementations(oldMethod, newMethod);
        }
    });
}

// 觸發(fā)按鈕事件時(shí)會(huì)調(diào)用
- (void)newSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
        self.timeInterVal = self.timeInterVal == 0? defaultInterval:self.timeInterVal;
        if (self.isExcuteEvent) return;
        if (self.timeInterVal > 0) {
            self.isExcuteEvent = true;
            [self performSelector:@selector(setIsExcuteEvent:) withObject:nil afterDelay:self.timeInterVal];
        }
    }
    [self newSendAction:action to:target forEvent:event];
}

// 動(dòng)態(tài)關(guān)聯(lián)屬性
- (NSTimeInterval)timeInterVal{
    return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setTimeInterVal:(NSTimeInterval)timeInterVal{
    objc_setAssociatedObject(self, @selector(timeInterVal), @(timeInterVal), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)setIsExcuteEvent:(BOOL)isExcuteEvent{
    objc_setAssociatedObject(self, @selector(isExcuteEvent), @(isExcuteEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isExcuteEvent{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
@end

》》》 》》》 2. 使用

#import "UIButton+FQTimeButton.h"

-(UIButton *)saveButton{
    if(!_saveButton){
        _saveButton=[UIButton new];
        [_saveButton setTimeInterVal:3.5];    // 設(shè)置3.5秒內(nèi)不可重復(fù)點(diǎn)擊
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_saveButton.titleLabel setFont:kFontBold(14)];
        [_saveButton setBackgroundColor:[UIColor colorWithHex:0xFB5752]];
        [_saveButton.layer setMasksToBounds:true];
        [_saveButton.layer setCornerRadius:kEstimateWidth(50)/2.0];
        [_saveButton addTarget:self action:@selector(fq_handleSave:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}
  1. 擴(kuò)大按鈕的點(diǎn)擊區(qū)域

創(chuàng)建一個(gè)繼承自UIButton的子類,需要擴(kuò)大點(diǎn)擊區(qū)域時(shí),繼承該類

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    CGRect rect=self.bounds;
    CGFloat width=rect.size.width;
    CGFloat height=rect.size.height;
    rect=CGRectInset(rect, -width*0.5, -height*0.5);  // 寬高各擴(kuò)大了一半
    return CGRectContainsPoint(rect, point);
}

2. 手勢(shì)

  1. 系統(tǒng)自帶手勢(shì)
  1. 點(diǎn)擊手勢(shì) UITapGestureRecognizer
    UITapGestureRecognizer *tapG=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    tapG.numberOfTapsRequired=1;    // 點(diǎn)擊次數(shù)(默認(rèn):1)
    tapG.numberOfTouchesRequired=1; // 點(diǎn)擊手指數(shù)(默認(rèn):1)
    [imgV1 addGestureRecognizer:tapG];  // imgV 必須設(shè)置為可交互

-(void)handleTap:(UITapGestureRecognizer *)tapG{
    //
    UIImageView *imgV=(UIImageView *)tapG.view;
}
  1. 長按手勢(shì) UILongPressGestureRecognizer
    // 長按手勢(shì)
    UILongPressGestureRecognizer *longPressG=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
    longPressG.numberOfTouchesRequired=1;   // 手指數(shù)(默認(rèn):1)
    // longPressG.numberOfTapsRequired=1;      // 點(diǎn)擊數(shù)(默認(rèn):0)
    longPressG.minimumPressDuration=1;      // 最小的長按時(shí)間(默認(rèn):0.5)
    longPressG.allowableMovement=10;        // 最大的移動(dòng)距離(默認(rèn):10ps)
    [[UIImageView new]addGestureRecognizer:longPressG];

-(void)handleLongPress:(UILongPressGestureRecognizer *)longPressG{
    //
    UIImageView *imgV=(UIImageView *)longPressG.view;
    if(tapG.state==UIGestureRecognizerStateEnded){

    }else if(tapG.state==UIGestureRecognizerStateBegan){

    }
}
  1. 滑動(dòng)手勢(shì) UIPanGestureRecognizer
    // 拖動(dòng)手勢(shì)
    UIPanGestureRecognizer *panG=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [panG setMaximumNumberOfTouches:2]; // 最小手指數(shù)
    [panG setMinimumNumberOfTouches:1]; // 最大手指數(shù)
    [[UIImageView new]addGestureRecognizer:panG];

-(void)handlePan:(UIPanGestureRecognizer *)panG{
    //
    UIButton *itemButton=(UIButton *)panG.view;
    CGPoint transP=[panG translationInView:self];
    NSLog(@"%f  %f",transP.x,transP.y);
    
    //
    if(transP.x>0&&transP.x<294-95){
        [UIView animateWithDuration:0.1 animations:^{
            [itemButton setTransform:CGAffineTransformMakeTranslation(transP.x, 0)];
        }];
    }else if(transP.x>=294){
        [self.subject sendNext:@(100)];
    }
    
    //
    if(panG.state==UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:0.1 animations:^{
            [itemButton setTransform:CGAffineTransformIdentity];
        }];
    }
  
    // [panG setTranslation:CGPointZero inView:self.view]; // reset
}
  1. 掃動(dòng)手勢(shì) UISwipeGestureRecognizer
    // 掃動(dòng)手勢(shì)
    UISwipeGestureRecognizer *swipeG=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
    swipeG.numberOfTouchesRequired=1;   // 手指數(shù)(默認(rèn):1)
    swipeG.direction=UISwipeGestureRecognizerDirectionLeft; // 掃動(dòng)方向(如果多個(gè)方向,則添加多個(gè)手勢(shì))
    [[UIImageView new]addGestureRecognizer:swipeG];

-(void)handleSwipe:(UISwipeGestureRecognizer *)swipeG{
    //
    UIImageView *imgV=(UIImageView *)swipeG.view;
}
  1. 縮放手勢(shì) UIPinchGestureRecognizer
    // 縮放手勢(shì)
    UIPinchGestureRecognizer *pinchG=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
    [pinchG setScale:0.5];  // 縮放比例
    // pinchG.velocity(readOnly)
    [[UIImageView new]addGestureRecognizer:pinchG];

-(void)handlePinch:(UIPinchGestureRecognizer *)pinchG{
    //
    UIImageView *imgV=(UIImageView *)pinchG.view;
    // pinchG.scale  
}
  1. 旋轉(zhuǎn)手勢(shì) UIRotationGestureRecognizer
    // 旋轉(zhuǎn)手勢(shì)
    UIRotationGestureRecognizer *roteG=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRote:)];
    [roteG setRotation:M_PI_2]; // 旋轉(zhuǎn)角度
    // roteG.velocity
    [[UIImageView new]addGestureRecognizer:roteG];

-(void)handleRote:(UIRotationGestureRecognizer *)roteG{
    //
    UIImageView *imgV=(UIImageView *)roteG.view;
    // roteG.rotation
}
  1. 自定義手勢(shì)

: UIGestureRecognizer

#import <UIKit/UIKit.h>
@interface CustomGestureRecognizer : UIGestureRecognizer
@end

#import "CustomGestureRecognizer.h"
#import <UIkit/UIGestureRecognizerSubclass.h>
@implementation CustomGestureRecognizer

-(instancetype)initWithTarget:(id)target action:(SEL)action{
    return [super initWithTarget:target action:action];
}

// 開始觸摸時(shí)調(diào)用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 觸摸移動(dòng)時(shí)調(diào)用
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 觸摸結(jié)束時(shí)調(diào)用
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 觸摸取消時(shí)調(diào)用
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
@end

基類 UIGestureRecognizer

UIGestureRecognizer : NSObject   
    所有手勢(shì)的基類,不直接使用

    // 創(chuàng)建手勢(shì) (添加self為target)
    UIGestureRecognizer *g=[[UIGestureRecognizer alloc]initWithTarget:self action:@selector(handleG)];
    // 添加 其他VC的targetAction
    [g addTarget:OtherVC action:@selector(handleG)];
    // 移除 其他VC的targetAction
    [g removeTarget:OtherVC action:@selector(handleG)];

    // 獲取 手勢(shì)操作的View(readonly)
    UIView *view=g.view;
    // 獲取 手勢(shì)的當(dāng)前狀態(tài)(readOnly)
    UIGestureRecognizerState state=g.state;
    // 獲取 手指數(shù)(readonly)
    NSUInteger numberOfTouches=g.numberOfTouches;
    // 獲取 觸摸點(diǎn)的坐標(biāo)
    CGPoint touchPoint2=[g locationInView:g.view];
    // 獲取 指定觸摸點(diǎn)的坐標(biāo)
    CGPoint touchPoint=[g locationOfTouch:0 inView:g.view];
    // 當(dāng)兩個(gè)手勢(shì)有【沖突】時(shí),手勢(shì)1只會(huì)在手勢(shì)2失敗時(shí)才會(huì)有效
    [g1 requireGestureRecognizerToFail:g2];

    // 設(shè)置 手勢(shì)名
    [g setName:@""];
    // 獲取 手勢(shì)名
    NSString *name=g.name;
    // 設(shè)置 是否可用(默認(rèn):true)
    [g setEnabled:true];
    // 獲取 是否可用
    BOOL isEnabled=g.isEnabled;
    // 設(shè)置 是否同時(shí)考慮不同手勢(shì)(true會(huì)忽略不同手勢(shì))
    [g setRequiresExclusiveTouchType:true];
    // 設(shè)置 允許觸摸的類型
    [g setAllowedTouchTypes:@[[NSNumber numberWithInt:0]]];
    // 設(shè)置 允許按壓的類型
    [g setAllowedPressTypes:@[[NSNumber numberWithInt:0]]];


手勢(shì)識(shí)別和觸摸事件相互獨(dú)立,但可通過以下3個(gè)屬性互相影響:
     在3個(gè)屬性都處于默認(rèn)值的情況下,如果觸摸window,首先由window上最先符合條件的控件(該控件記為hit-test view)接收到該touch并觸發(fā)觸摸事件touchesBegan。同時(shí)如果某個(gè)控件的手勢(shì)識(shí)別器接收到了該touch,就會(huì)進(jìn)行識(shí)別。手勢(shì)識(shí)別成功之后發(fā)送觸摸事件touchesCancelled給hit-testview,hit-test view不再響應(yīng)touch。
    /*
     默認(rèn)為YES,這種情況下當(dāng)手勢(shì)識(shí)別器識(shí)別到touch之后,會(huì)發(fā)送touchesCancelled給hit-testview以取消hit-test view對(duì)touch的響應(yīng),這個(gè)時(shí)候只有手勢(shì)識(shí)別器響應(yīng)touch。
     若設(shè)置成NO時(shí),手勢(shì)識(shí)別器識(shí)別到touch之后不會(huì)發(fā)送touchesCancelled給hit-test,這個(gè)時(shí)候手勢(shì)識(shí)別器和hit-test view均響應(yīng)touch。
    */
    [g setCancelsTouchesInView:true];
    /*
     默認(rèn)是NO,這種情況下當(dāng)發(fā)生一個(gè)touch時(shí),手勢(shì)識(shí)別器先捕捉到到touch,然后發(fā)給hit-testview,兩者各自做出響應(yīng)。
    若設(shè)置為YES,手勢(shì)識(shí)別器在識(shí)別的過程中(注意是識(shí)別過程),不會(huì)將touch發(fā)給hit-test view,即hit-testview不會(huì)有任何觸摸事件。只有在識(shí)別失敗之后才會(huì)將touch發(fā)給hit-testview,這種情況下hit-test view的響應(yīng)會(huì)延遲約0.15ms。
    */
    [g setDelaysTouchesBegan:true];
    /*
     默認(rèn)為YES。這種情況下發(fā)生一個(gè)touch時(shí),在手勢(shì)識(shí)別成功后,發(fā)送給touchesCancelled消息給hit-testview,手勢(shì)識(shí)別失敗時(shí),會(huì)延遲大概0.15ms,期間沒有接收到別的touch才會(huì)發(fā)送touchesEnded。
     若設(shè)置為NO,則不會(huì)延遲,即會(huì)立即發(fā)送touchesEnded以結(jié)束當(dāng)前觸摸。
    */
    [g setDelaysTouchesEnded:true];

dele

    // dele. <UIGestureRecognizerDelegate>
    [g setDelegate:self];


#pragma mark dele -UIGestureRecognizerDelegate
/*
 是否允許手勢(shì)
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return true;
}
/*
是否支持多手勢(shì)(解決有水平方向滾動(dòng)的ScrollView時(shí)邊緣返回手勢(shì)失效的問題)
  false默認(rèn),識(shí)別到第一個(gè)手勢(shì)則停止識(shí)別第二個(gè)手勢(shì)
  true,則識(shí)別到第一個(gè)手勢(shì),還會(huì)去識(shí)別第二個(gè)手勢(shì)
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
/*
 當(dāng)?shù)谝粋€(gè)手勢(shì)gestureRecognizer和第二個(gè)手勢(shì)otherGestureRecognizer發(fā)生沖突時(shí),第二個(gè)失效
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
/*
 當(dāng)?shù)谝粋€(gè)手勢(shì)和第二個(gè)手勢(shì)發(fā)生沖突時(shí),第一個(gè)失效
*/
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return true;
}
/*
 是否允許 手勢(shì)識(shí)別(默認(rèn):true)
*/
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return false;   // 禁止識(shí)別
}
/*
 開始識(shí)別手勢(shì)時(shí)調(diào)用(可用于:控件某范圍有效)
*/
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return true;
}
  1. 常用
  1. UIViewController 或 UIView 中可覆寫
// UIViewController:UIResponder

// 開始觸摸后調(diào)用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 觸摸并移動(dòng)后調(diào)用
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 觸摸結(jié)束后調(diào)用
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 觸摸取消后調(diào)用
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
  1. 獲取到側(cè)滑手勢(shì)
    // 獲取側(cè)滑手勢(shì),側(cè)滑手勢(shì)繼承于:UIPanGestureRecognizer
    NSArray *gestureArray = self.navigationController.view.gestureRecognizers;
    for (UIGestureRecognizer *gesture in gestureArray) {
        if ([gesture isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
        }
     }
  1. 在VC禁止側(cè)滑返回上一頁
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,753評(píng)論 4 61
  • JAVA虛擬機(jī)(JVM)是啥? Java虛擬機(jī)(英語:Java Virtual Machine,縮寫為JVM),一...
    evil_ice閱讀 709評(píng)論 0 0
  • 說句實(shí)話,在如今商品經(jīng)濟(jì)大潮中聽聞?wù)l誰誰又發(fā)財(cái)了,誰誰誰又開了什么店,誰誰誰又開始新的創(chuàng)業(yè),我聽后初始興奮,繼而平...
    亦農(nóng)閱讀 476評(píng)論 13 11
  • 從3月6日關(guān)注橙子成長聯(lián)盟以來,每天堅(jiān)持打卡報(bào)到,記錄三件事,然后順帶完成一些小確幸,到今天已經(jīng)是第22天了。而能...
    旼旪閱讀 387評(píng)論 0 1
  • 漫茜閱讀 573評(píng)論 0 0

友情鏈接更多精彩內(nèi)容