UIGestureRecognizer
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供酒泉網(wǎng)站建設(shè)、酒泉做網(wǎng)站、酒泉網(wǎng)站設(shè)計、酒泉網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、酒泉企業(yè)網(wǎng)站模板建站服務(wù),十年酒泉做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。利用手勢識別器,能夠輕松識別用戶在某個view上面做一些常見的手勢
UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢
UITapGestureRecognizer 敲擊
UIPinchGestureRecognizer 捏合手勢
UIPanGestureRecognizer 拖拽
UISwipeGestureRecognizer 輕掃
UIRotationGestureRecognizer 旋轉(zhuǎn)
UILongPressGestureRecognizer 長按
手勢識別器使用的一般步驟 (以敲擊手勢為例)
創(chuàng)建手勢識別器對象
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init];
設(shè)置手勢識別器對象的具體屬性 :
//連續(xù)敲擊2次且兩根手指敲擊時響應(yīng) tap.numberOfTapsRequired = 2; tap.numberOfTouchesRequired = 2;
添加手勢識別器到view上 :
[self.iconView addGestureRecognizer:tap];
監(jiān)聽手勢的觸發(fā) :
[tap addTarger:self action:@selector(tapIconView:)];
實現(xiàn)觸發(fā)的方法
- (void) tapIconView:(UITapGestureRecognizer*) tapGR { NSLog(@"tap響應(yīng)"); }
手勢識別器的狀態(tài)
手勢識別器的代理
代理協(xié)議
//是否接受這個touch對象(默認(rèn)返回YES),這個代理方法在一個觸摸事件產(chǎn)生時先調(diào)用 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldReceiveTouch:(UITouch*) touch; //確定兩個類似的手勢識別器是否同時處理 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer;
示例:UITabBarController的滑動手勢控制
1)UITabBarController子類,保存所有子控制器的截圖
添加UIImage對象數(shù)組
@property (nonatomic, strong) NSMutableArray* picArray;
數(shù)組初始化為空對象
NSMutableArray * arr1 = [NSMutableArray array]; for ( int i =0; i重寫selectedIndex的setter方法:切換子控制器前,截圖保存
- (void)setSelectedIndex:(NSUInteger)selectedIndex { //截圖操作 float width = [UIScreen mainScreen].bounds.size.width; float height = [UIScreen mainScreen].bounds.size.height; UIGraphicsBeginImageContext(CGSizeMake(width, height)); CGContextRef context = UIGraphicsGetCurrentContext(); [[UIApplication sharedApplication].keyWindow.layer renderInContext:context]; UIImage *p_w_picpath = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //截圖保存到截圖數(shù)組 NSLog(@"selectedIndex=%ld", self.selectedIndex); self.picArray[self.selectedIndex] = p_w_picpath; [super setSelectedIndex:selectedIndex]; }提供方法,返回當(dāng)前子控制器的前后兩個子控制器的截圖
- (UIImage *) p_w_picpathForCurrentViewControllerLeft { if ( self.selectedIndex == 0 ) { return nil; } return self.picArray[self.selectedIndex-1]; } - (UIImage *) p_w_picpathForCurrentViewControllerRight { if ( self.selectedIndex == self.viewControllers.count - 1 ) { return nil; } return self.picArray[self.selectedIndex+1]; }2)添加子控制器的共用父類
UITabBarController的所有子控制器的類型就是繼承自一個共用父類
父類中添加滑動手勢識別器
UIPanGestureRecognizer * gr1 = [[UIPanGestureRecognizer alloc] init]; [gr1 addTarget:self action:@selector(panGR:)]; [self.view addGestureRecognizer:gr1];父類中添加UIImageView屬性,用于存儲當(dāng)前頁面左右兩個截圖p_w_picpathView
@property (nonatomic, weak) UIImageView * ivLeft; @property (nonatomic, weak) UIImageView * ivRight;手勢識別器:手勢開始時,當(dāng)前view左右放置截圖
UIImage * imgLeft = [((ViewController*)self.tabBarController) p_w_picpathForCurrentViewControllerLeft]; if ( imgLeft == nil ) { self.ivLeft = nil; } else { if ( [imgLeft isKindOfClass:[NSNull class]] ) { //一張白***片 imgLeft = [UIImage p_w_picpathNamed:@"white_bg"]; } UIImageView * ivLeft = [[UIImageView alloc] initWithImage:imgLeft]; self.ivLeft = ivLeft; [self.view addSubview:ivLeft]; self.ivLeft.frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height); } UIImage * imgRight = [((ViewController*)self.tabBarController) p_w_picpathForCurrentViewControllerRight]; if ( imgRight == nil ) { self.ivRight = nil; } else { if ( [imgRight isKindOfClass:[NSNull class]] ) { imgRight = [UIImage p_w_picpathNamed:@"white_bg"]; } UIImageView * ivRight= [[UIImageView alloc] initWithImage:imgRight]; self.ivRight = ivRight; [self.view addSubview:self.ivRight]; self.ivRight.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height); }手勢識別器:手勢滑動式時,當(dāng)前view隨之滑動
if ( p.x > 0 && self.ivLeft == nil ) { return ; } if ( p.x < 0 && self.ivRight == nil ) { return ; } self.view.transform = CGAffineTransformMakeTranslation(p.x, 0);手勢識別器,手勢結(jié)束時,根據(jù)位置確定是否切換子控制器
if ( self.view.transform.tx > self.view.frame.size.width/2 && self.ivLeft != nil ) { self.view.transform = CGAffineTransformIdentity; self.tabBarController.selectedIndex--; if ( self.ivLeft != nil ) { [self.ivLeft removeFromSuperview]; self.ivLeft = nil; } if ( self.ivRight != nil ) { [self.ivRight removeFromSuperview]; self.ivRight = nil; } return ; } if ( self.view.transform.tx < -self.view.frame.size.width/2 && self.ivRight != nil ) { self.view.transform = CGAffineTransformIdentity; self.tabBarController.selectedIndex++; if ( self.ivLeft != nil ) { [self.ivLeft removeFromSuperview]; self.ivLeft = nil; } if ( self.ivRight != nil ) { [self.ivRight removeFromSuperview]; self.ivRight = nil; } return ; } [UIView animateWithDuration:0.5 animations:^{ self.view.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { if ( self.ivLeft != nil ) { [self.ivLeft removeFromSuperview]; self.ivLeft = nil; } if ( self.ivRight != nil ) { [self.ivRight removeFromSuperview]; self.ivRight = nil; } }];另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享題目:UIKit框架(16)手勢識別器-創(chuàng)新互聯(lián)
本文URL:http://weahome.cn/article/dedech.html