热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

iOS实现侧滑栏效果

这篇文章主要为大家详细介绍了iOS实现侧滑栏效果,点击侧边拉出相应菜单,感兴趣的小伙伴们可以参考一下

效果

 

源码: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 &#63; (_mainView.x = gap) : (_mainView.x = 0);
   }];
  }
 }
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
遇遇yuyu
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有