iOS實(shí)現(xiàn)側(cè)滑欄效果
效果

源碼: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) {
// 過(guò)濾掉向左側(cè)滑過(guò)頭的情形
_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) {
// 過(guò)濾掉向左側(cè)滑過(guò)頭的情形
_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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼
本篇文章主要介紹了iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
iOS利用CoreImage實(shí)現(xiàn)人臉識(shí)別詳解
OS的人臉識(shí)別從iOS 5(2011)就有了,不過(guò)一直沒怎么被關(guān)注過(guò)。人臉識(shí)別API允許開發(fā)者不僅可以檢測(cè)人臉,也可以檢測(cè)到面部的一些特殊屬性,比如說(shuō)微笑或眨眼。下面這篇文章主要給大家介紹了iOS利用CoreImage實(shí)現(xiàn)人臉識(shí)別的相關(guān)資料,需要的朋友可以參考下。2017-05-05
IOS開發(fā)代碼分享之獲取啟動(dòng)畫面圖片的string
本文是IOS開發(fā)代碼分享系列的第一篇文章,這里分享下獲取啟動(dòng)畫面圖片的string的代碼,本代碼支持 iPhone 6 以下. 支持 iPhone 及 iPad,非常實(shí)用,希望對(duì)大家有所幫助2014-09-09
使用ARM匯編破解iOS程序基礎(chǔ)知識(shí)分享
最近對(duì)iOS逆向工程很感興趣。但查到的資料中都涉及到有ARM匯編,但都只是很泛地用到,并沒有對(duì)iOS上的ARM匯編進(jìn)行比較詳細(xì)的講解。因此,經(jīng)過(guò)一系列的學(xué)習(xí)對(duì)iOS下的ARM有了一定的理解。在此打算用幾篇文字記錄下來(lái), 限于本人水平有限,如有錯(cuò)誤請(qǐng)不吝賜教。2015-11-11
Objective-C處理空字符串和頁(yè)面?zhèn)髦导白远x拷貝
這篇文章主要介紹了Objective-C處理空字符串和頁(yè)面?zhèn)髦导白远x拷貝的相關(guān)方法,在iOS應(yīng)用項(xiàng)目開發(fā)中經(jīng)常會(huì)用到,需要的朋友可以參考下2016-01-01
iOS開發(fā)image背景圖片拉伸問(wèn)題解決分析
這篇文章主要為大家介紹了iOS開發(fā)image背景圖片拉伸問(wèn)題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

