ios手勢複習值之換圖片-轉場動畫(純程式碼)

來源:互聯網
上載者:User

標籤:

目標:實現通過手勢進行圖片的切換   通過左掃右掃 來實現(純程式碼)

添加三個屬性 1uiImageView 用來顯示圖片的view 

      2 index 用來表示圖片的索引

      3 ISLeft 判斷是不是向左滑  

下邊是詳細的代碼:

- (void)viewDidLoad {    [super viewDidLoad];    self.index = 0;    self.ISLeft = YES;    _imageView = [[UIImageView alloc]initWithFrame:self.view.frame];   // _imageView.backgroundColor = [UIColor redColor];    _imageView.contentMode = UIViewContentModeScaleAspectFit;//合適的大小        self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png",_index]];        [self.view addSubview:_imageView];            //使用者互動設定    self.imageView.userInteractionEnabled = YES;    //添加掃動得手勢    UISwipeGestureRecognizer *swipL = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];    swipL.direction = UISwipeGestureRecognizerDirectionLeft;    [self.imageView addGestureRecognizer:swipL];            UISwipeGestureRecognizer *swipR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];    swipR.direction = UISwipeGestureRecognizerDirectionRight;    [self.imageView addGestureRecognizer:swipR];    }

設定轉場動畫 :在手勢裡邊進行實現 

手勢的實現以及轉場動畫:

-(void)swip:(UISwipeGestureRecognizer *)sender{    if(sender.direction == UISwipeGestureRecognizerDirectionLeft)    {        if(self.index>=0)        {            if(self.index>0)            {                self.index--;            }            else                            {                self.index =3;            }            self.ISLeft = YES;        }    }    else    {        self.ISLeft = NO;        if(self.index<3)        {            self.index++;            if(self.index ==3)            {                self.index =0;            }        }    }        //轉場動畫    CATransition *trans = [[CATransition alloc]init];  //轉場動畫的類型    trans.type [email protected]"push";    //判斷是不是向左滑    if(self.ISLeft)    {        trans.subtype = kCATransitionFromTop;    }    else    {        trans.subtype = kCATransitionFromBottom;    }    trans.duration = 0.5f;    [self.imageView.layer addAnimation:trans forKey:@"trans"];    //切換圖畫    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",_index]];    }

 

ios手勢複習值之換圖片-轉場動畫(純程式碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.