亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

UISwipeGestureRecognizer ---手指動(dòng)作

系統(tǒng) 1610 0

tap是指輕觸手勢(shì)。類似鼠標(biāo)操作的點(diǎn)擊。從iOS 3.2版本開始支持完善的手勢(shì)api:

  • tap:輕觸
  • long press:在一點(diǎn)上長(zhǎng)按
  • pinch:兩個(gè)指頭捏或者放的操作
  • pan:手指的拖動(dòng)
  • swipe:手指在屏幕上很快的滑動(dòng)
  • rotation:手指反向操作

這為開發(fā)者編寫手勢(shì)識(shí)別操作,提供了很大的方便,想想之前用android寫手勢(shì)滑動(dòng)的代碼( 編寫android簡(jiǎn)單的手勢(shì)切換視圖示例 ),尤其感到幸福。

這里寫一個(gè)簡(jiǎn)單的tap操作。在下面視圖的藍(lán)色視圖內(nèi)增加對(duì)tap的識(shí)別:

image

?

當(dāng)用手指tap藍(lán)色視圖的時(shí)候,打印日志輸出:

image

代碼很簡(jiǎn)單,首先要聲明tap的recognizer:

UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[infoView addGestureRecognizer:recognizer];
[recognizer release];

在這里:

  • initWithTarget:self,要引用到Controller,因?yàn)橐话氵@部分代碼寫在controller中,用self;
  • action:@selector(handleTapFrom:),賦值一個(gè)方法名,用于當(dāng)手勢(shì)事件發(fā)生后的回調(diào);
  • [infoView addGestureRecognizer:recognizer],為view注冊(cè)這個(gè)手勢(shì)識(shí)別對(duì)象,這樣當(dāng)手指在該視圖區(qū)域內(nèi),可引發(fā)手勢(shì),之外則不會(huì)引發(fā)

對(duì)應(yīng)的回調(diào)方法:

-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer{
??? NSLog(@">>>tap it");
}

controller相關(guān)方法完整的代碼(包含了一些與本文無關(guān)的視圖構(gòu)建代碼):

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
??? //去掉最頂端的狀態(tài)攔
??? [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
???
??? UIImage *image=[UIImage imageNamed:@"3.jpg"];
???
??? //創(chuàng)建背景視圖
??? self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
??? UIImageView *backgroudView=[[UIImageView alloc] initWithImage:image];
??? [self.view addSubview:backgroudView];
???
??? /*
??? UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 1024-70, 768, 70)];
??? toolBar.alpha=0.8;
??? toolBar.tintColor = [UIColor colorWithRed:.3 green:.5 blue:.6 alpha:.1];
???
??? NSArray *items=[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithTitle:@"test" style:UIBarButtonItemStyleDone target:self action:nil],nil];
??? toolBar.items=items;
??? [self.view addSubview:toolBar];
??? */
???
??? UIView *bottomView=[[UIView alloc]? initWithFrame:CGRectMake(0, 1024-70, 768, 70)];
??? bottomView.backgroundColor=[UIColor grayColor];
??? bottomView.alpha=0.8;
???
??? //UIButton *backButton=[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
??? UIButton *backButton=[UIButton buttonWithType: UIButtonTypeRoundedRect];
??? [backButton setTitle:@"ok" forState:UIControlStateNormal];
??? backButton.frame=CGRectMake(10, 15, 100, 40);
???
??? [bottomView addSubview:backButton];
???
??? [self.view addSubview:bottomView];
???
??? UIView *infoView=[[UIView alloc] initWithFrame:CGRectMake(200, 700, 768-400, 70)];
??? infoView.backgroundColor=[UIColor blueColor];
??? infoView.alpha=0.6;
??? infoView.layer.cornerRadius=6;
??? infoView.layer.masksToBounds=YES;
??? [self.view addSubview:infoView];
???
??? UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
??? [infoView addGestureRecognizer:recognizer];
??? [recognizer release];
}

-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer{
??? NSLog(@">>>tap it");
}

?

?

?

?

翻頁效果,類似下面的樣子:

image image

在電子書應(yīng)用中會(huì)很常見。這里需要兩個(gè)要點(diǎn):

  • 翻頁動(dòng)畫
  • 手勢(shì)上下輕掃(swipe)的處理

?

先說一下輕掃(swipe)的實(shí)現(xiàn),可以參考 編寫簡(jiǎn)單的手勢(shì)示例:Tap 了解手勢(shì)種類。

在viewDidLoad方法中注冊(cè)了對(duì)上、下、左、右四個(gè)方向輕松的處理方法:

- (void)viewDidLoad {
???
??? UISwipeGestureRecognizer *recognizer;
???
??? recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
??? [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
??? [[self view] addGestureRecognizer:recognizer];
??? [recognizer release];
???
??? recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
??? [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
??? [[self view] addGestureRecognizer:recognizer];
??? [recognizer release];
???
??? recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
??? [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
??? [[self view] addGestureRecognizer:recognizer];
??? [recognizer release];
???
??? recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
??? [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
??? [[self view] addGestureRecognizer:recognizer];
??? [recognizer release];
???
???
??? [super viewDidLoad];

?

可以看到,都是同一個(gè)方法,handleSwipeFrom。

在該方法中,再識(shí)別具體是哪個(gè)方向的輕掃手勢(shì),比如判斷是向下的輕掃:

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
??? NSLog(@"Swipe received.");
???
??? if (recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
??????? NSLog(@"swipe down");

判斷是向上的輕掃:

if (recognizer.direction==UISwipeGestureRecognizerDirectionUp) {
??? NSLog(@"swipe up");

有關(guān)動(dòng)畫的處理,比如向下(往回)翻頁,類似這樣:

[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];

[currentView removeFromSuperview];
[self.view addSubview:contentView];

[UIView commitAnimations];

向上(向前)翻頁,只需改為:

[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

[currentView removeFromSuperview];
[self.view addSubview:contentView];

[UIView commitAnimations];

如果是電子書,還需要考慮一個(gè)問題,就是有多個(gè)頁面(圖形),比如50頁。那么需要有一個(gè)數(shù)據(jù)結(jié)構(gòu)來保存這些頁面的圖片路徑:

  • objc數(shù)據(jù)結(jié)構(gòu),比如數(shù)組
  • sqlite數(shù)據(jù)庫表

這樣,寫一套翻頁代碼和加載什么圖形之間就可以解耦。

本文示例使用的是數(shù)組,類似這樣:

pages=[[NSArray alloc] initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",
???????????????? nil];

圖片保存在resources下。

為了能讓上頁下頁翻頁的時(shí)候找到關(guān)聯(lián)的頁面,采用了如下機(jī)制:

  • 將圖片封裝為UIImageView顯示
  • 可以為UIImageView設(shè)置一個(gè)tag值,值為數(shù)組下標(biāo)+1
  • 這樣,上級(jí)view有方法能根據(jù)tag查詢到UIImageView,比如:UIView *currentView=[self.view viewWithTag:currentTag];
  • 設(shè)置一個(gè)成員變量currentTag保存當(dāng)前的tag值

比如這樣,當(dāng)應(yīng)用加載的時(shí)候顯示第一頁:

??? currentTag=1;
???
??? NSString *path = [[NSBundle mainBundle] pathForResource:@"pageflip1" ofType:@"mp3"];
??? player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
???
??? //[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
??? [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
??? UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];?
??? [contentView setImage:[UIImage imageNamed:[pages objectAtIndex:(currentTag-1)]]];?
??? [contentView setUserInteractionEnabled:YES];
??? contentView.tag=currentTag;

在翻頁時(shí)的處理:

if (currentTag<[pages count]) {
??? UIView *currentView=[self.view viewWithTag:currentTag];
??? currentTag++;
???
??? UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
??? [contentView setImage:[UIImage imageNamed:[pages objectAtIndex:(currentTag-1)]]];?
??? [contentView setUserInteractionEnabled:YES];
??? contentView.tag=currentTag;
???
??? [UIView beginAnimations:@"animationID" context:nil];
??? [UIView setAnimationDuration:0.7f];
??? [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
??? [UIView setAnimationRepeatAutoreverses:NO];
??? [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
???
??? [currentView removeFromSuperview];
??? [self.view addSubview:contentView];
???
??? [UIView commitAnimations];

UISwipeGestureRecognizer ---手指動(dòng)作


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲精品久久久久综合网 | 四虎国产一区 | 日产精品一二三四区国产 | 久久国产精品亚洲一区二区 | 国产成人免费在线视频 | 欧美一级片在线视频 | 国产精品亚洲综合一区在线观看 | 日韩色区 | 亚洲国产美女视频 | 日本一区二区三区在线 观看网站 | 欧美日韩亚洲国产一区二区综合 | 色偷偷亚洲第一综合 | 欧美成人性色生活片天天看 | 91久久亚洲精品一区二区 | 国产美女a做受大片在线观看 | 奇米777狠狠色噜噜狠狠狠 | 四虎网站1515hh四虎免费 | 婷婷国产偷v国产偷v亚洲 | 在线免费观看毛片 | 国产精品天天操 | 伊人网在线视频 | 亲热网站| 在线观看亚洲网站 | 日本不卡视频免费 | 亚洲久草 | 国产成人亚洲精品一区二区在线看 | 国内视频在线 | 97影院在线观看 | 欧美xxxxbbbb在线播放 | 97色伦图片97色伦图影院久久 | 亚洲一区日韩 | 国产成人精品一区二区仙踪林 | 欧美一区二区三区久久久 | 91精品免费久久久久久久久 | 国产精品免费视频一区一 | 99re这里有免费视频精品 | 四虎 2022 永久网站 | 色综合久久久久久 | 日本欧美一区二区三区乱码 | 亚洲国产成人久久综合野外 | 又粗又大的机巴好爽欧美 |