iOS實(shí)現(xiàn)側(cè)滑欄效果
更新時(shí)間:2016年08月10日 11:28:24 作者:YouXianMing
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)側(cè)滑欄效果,點(diǎn)擊側(cè)邊拉出相應(yīng)菜單,感興趣的小伙伴們可以參考一下
效果
源碼:https://github.com/YouXianMing/iOS-Project-Examples 中的 SideViewController
// // ViewController.m // SideViewController // // Created by YouXianMing on 16/6/6. // Copyright © 2016年 YouXianMing. All rights reserved. // #import "ViewController.h" #import "LeftViewController.h" #import "MainViewController.h" #import "UIView+SetRect.h" @interface ViewController () { CGFloat _screenWidth; } @property (nonatomic, strong) UIPanGestureRecognizer *panGesture; @property (nonatomic) CGPoint panBeginPoint; @property (nonatomic, strong) LeftViewController *leftViewController; @property (nonatomic, strong) UIView *leftView; @property (nonatomic, strong) MainViewController *mainViewController; @property (nonatomic, strong) UIView *mainView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Init some value. _screenWidth = Width; // Add backgroundView. UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:self.view.bounds]; backgroundView.image = [UIImage imageNamed:@"back"]; [self.view addSubview:backgroundView]; // LeftViewController self.leftViewController = [[LeftViewController alloc] init]; self.leftView = self.leftViewController.view; [self.view addSubview:self.leftView]; // MainViewController self.mainViewController = [[MainViewController alloc] init]; self.mainView = self.mainViewController.view; [self.view addSubview:self.mainView]; // Pan gesture. self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureEvent:)]; [self.mainView addGestureRecognizer:self.panGesture]; } - (void)panGestureEvent:(UIPanGestureRecognizer *)gesture { CGPoint translation = [gesture translationInView:gesture.view]; CGPoint velocity = [gesture velocityInView:gesture.view]; CGFloat gap = _screenWidth / 3.f * 2; CGFloat sensitivePosition = _screenWidth / 2.f; if (velocity.x < 0 && _mainView.x <= 0) { // 過濾掉向左側(cè)滑過頭的情形 _mainView.x = 0.f; } else { if (gesture.state == UIGestureRecognizerStateBegan) { // 開始 _panBeginPoint = translation; if (_mainView.x >= sensitivePosition) { _panBeginPoint.x -= gap; } } else if (gesture.state == UIGestureRecognizerStateChanged) { // 值變化 _mainView.x = translation.x - _panBeginPoint.x; if (_mainView.x <= 0) { // 過濾掉向左側(cè)滑過頭的情形 _mainView.x = 0.f; } } else if (gesture.state == UIGestureRecognizerStateEnded) { // 結(jié)束 [UIView animateWithDuration:0.20f animations:^{ _mainView.x >= sensitivePosition ? (_mainView.x = gap) : (_mainView.x = 0); }]; } } } @end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼
本篇文章主要介紹了iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04iOS利用CoreImage實(shí)現(xiàn)人臉識別詳解
OS的人臉識別從iOS 5(2011)就有了,不過一直沒怎么被關(guān)注過。人臉識別API允許開發(fā)者不僅可以檢測人臉,也可以檢測到面部的一些特殊屬性,比如說微笑或眨眼。下面這篇文章主要給大家介紹了iOS利用CoreImage實(shí)現(xiàn)人臉識別的相關(guān)資料,需要的朋友可以參考下。2017-05-05IOS開發(fā)代碼分享之獲取啟動(dòng)畫面圖片的string
本文是IOS開發(fā)代碼分享系列的第一篇文章,這里分享下獲取啟動(dòng)畫面圖片的string的代碼,本代碼支持 iPhone 6 以下. 支持 iPhone 及 iPad,非常實(shí)用,希望對大家有所幫助2014-09-09Objective-C處理空字符串和頁面?zhèn)髦导白远x拷貝
這篇文章主要介紹了Objective-C處理空字符串和頁面?zhèn)髦导白远x拷貝的相關(guān)方法,在iOS應(yīng)用項(xiàng)目開發(fā)中經(jīng)常會(huì)用到,需要的朋友可以參考下2016-01-01