iOS實現側滑欄效果_IOS

來源:互聯網
上載者:User

效果

 

源碼: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) {    // 過濾掉向左側滑過頭的情形  _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) {        // 過濾掉向左側滑過頭的情形    _mainView.x = 0.f;   }     } else if (gesture.state == UIGestureRecognizerStateEnded) {      // 結束   [UIView animateWithDuration:0.20f animations:^{        _mainView.x >= sensitivePosition ? (_mainView.x = gap) : (_mainView.x = 0);   }];  } }}@end

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.