欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

UISwipeGestureRecognizer ---手指動作

系統 1686 0

tap是指輕觸手勢。類似鼠標操作的點擊。從iOS 3.2版本開始支持完善的手勢api:

  • tap:輕觸
  • long press:在一點上長按
  • pinch:兩個指頭捏或者放的操作
  • pan:手指的拖動
  • swipe:手指在屏幕上很快的滑動
  • rotation:手指反向操作

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

這里寫一個簡單的tap操作。在下面視圖的藍色視圖內增加對tap的識別:

image

?

當用手指tap藍色視圖的時候,打印日志輸出:

image

代碼很簡單,首先要聲明tap的recognizer:

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

在這里:

  • initWithTarget:self,要引用到Controller,因為一般這部分代碼寫在controller中,用self;
  • action:@selector(handleTapFrom:),賦值一個方法名,用于當手勢事件發生后的回調;
  • [infoView addGestureRecognizer:recognizer],為view注冊這個手勢識別對象,這樣當手指在該視圖區域內,可引發手勢,之外則不會引發

對應的回調方法:

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

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

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
??? //去掉最頂端的狀態攔
??? [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
???
??? UIImage *image=[UIImage imageNamed:@"3.jpg"];
???
??? //創建背景視圖
??? 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

在電子書應用中會很常見。這里需要兩個要點:

  • 翻頁動畫
  • 手勢上下輕掃(swipe)的處理

?

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

在viewDidLoad方法中注冊了對上、下、左、右四個方向輕松的處理方法:

- (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];

?

可以看到,都是同一個方法,handleSwipeFrom。

在該方法中,再識別具體是哪個方向的輕掃手勢,比如判斷是向下的輕掃:

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

判斷是向上的輕掃:

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

有關動畫的處理,比如向下(往回)翻頁,類似這樣:

[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];

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

  • objc數據結構,比如數組
  • sqlite數據庫表

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

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

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

圖片保存在resources下。

為了能讓上頁下頁翻頁的時候找到關聯的頁面,采用了如下機制:

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

比如這樣,當應用加載的時候顯示第一頁:

??? 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;

在翻頁時的處理:

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 ---手指動作


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 在线视频 中文字幕 | 久久精品免费视频观看 | 日韩中文字幕视频在线 | 91视频免费网址 | 草久久免费视频 | 性视频网站免费 | 日韩在线观看中文字幕 | 日日夜夜操操操 | 亚洲三级在线 | 婷婷六月天 | 日本免费一区二区三区视频 | 一级观看免费完整版视频 | 国产在视频一区二区三区吞精 | 亚洲福利 | 日a在线| 一区二区三区在线免费观看 | 亚州 色毛片免费观看 | 亚洲一区二区三区四区精品 | 成年人在线视频网站 | 日本高清无吗 | 五月婷婷丁香综合网 | 午夜在线小视频 | 成人片网址| 国产成年网站 | 欧美一区二区三区四区视频 | 久久福利| 一本大道香蕉中文日本不卡高清二区 | 激情婷婷成人亚洲综合 | 免费在线观看的毛片 | 激情综合婷婷久久 | 亚洲专区区免费 | 91精品免费观看 | 搞黄网站免费观看 | 一级大片免费看 | 久草青娱乐 | 国产欧美一区二区精品久久久 | 99久久久精品国产一区二区 | 日本青草视频 | 国产精品久久久久影视青草 | 天天操天天拍 | 亚洲一区二区视频在线观看 |