iOS实现左右拖动抽屉效果

所属分类: 软件编程 / IOS 阅读数: 2025
收藏 0 赞 0 分享

本文实例介绍了iOS实现左右拖动抽屉效果,具体内容如下

利用了触摸事件滑动 touchesMoved: 来触发左右视图的出现和消失 利用loadView方法中添加view 在self.view载入前就把 左右中View都设置好frame 每一个方法都由单独的功能。

#import "DarwViewController.h"
@interface DarwViewController ()
@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *rightView;
@property (nonatomic, weak) UIView *mainView;
/**
 * 动画是否进行
 */
@property (nonatomic ,assign) BOOL animating;
 
@end
 
@implementation DarwViewController
- (void)viewDidLoad {
 [super viewDidLoad];
}
 
 
-(void)loadView
{
 self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
 //左边view
 UIView *leftView = [[UIView alloc]initWithFrame:self.view.frame];
 [self.view addSubview:leftView];
 leftView.backgroundColor= [UIColor redColor];
 self.leftView = leftView;
  
 //右边View
 UIView *rightView = [[UIView alloc]initWithFrame:self.view.frame];
 [self.view addSubview:rightView];
 rightView.backgroundColor= [UIColor blueColor];
 self.rightView = rightView;
  
 //主页面
 UIView *mainView = [[UIView alloc]initWithFrame:self.view.frame];
 [self.view addSubview:mainView];
 mainView.backgroundColor= [UIColor yellowColor];
 self.mainView = mainView;
  
  
 //KVO监听
 [self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}
/**
 * KVO回调方法 当mainView Frame值改变时触发
 *
 * @param keyPath <#keyPath description#>
 * @param object <#object description#>
 * @param change <#change description#>
 * @param context <#context description#>
 */
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
 if (self.animating) return; //如果mainView正在动画 就不执行
 if (self.mainView.frame.origin.x > 0 )
 {
  //X > 0 就隐藏右边View 显示左边View
  self.rightView.hidden = YES;
  self.leftView.hidden = NO;
 }
 else if (self.mainView.frame.origin.x < 0)
 {
  //X < 0 就隐藏左边View 显示右边VIew
  self.leftView.hidden = YES;
  self.rightView.hidden = NO;
 }
}
#pragma mark -- 触摸事件
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
 //获得触摸对象
 UITouch *touch = [touches anyObject];
  
 //获得当前触摸点
 CGPoint currentPoint = [touch locationInView:self.view];
 //获得上一个触摸点
 CGPoint previousPoint = [touch previousLocationInView:self.view];
  
 //计算x方向的偏移量
 CGFloat offsetX = currentPoint.x - previousPoint.x;
// 根据x的偏移量计算y的偏移量
 self.mainView.frame = [self rectWithOffsetX:offsetX];
  
}
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
/**
 * 计算主视图的frame
 *
 * @param offsetX x的偏移量
 *
 * @return 偏移后新的frame
 */
- (CGRect ) rectWithOffsetX:(CGFloat )offsetX
{
 //Y轴的偏移量
 CGFloat offsetY = (screenH *1/5) * (offsetX/screenW);
  
 //比例 :(用于宽高的缩放)
 CGFloat scale = (screenH - offsetY *2) / screenH;
 if (self.mainView.frame.origin.x < 0 )
 {
  //如果x是负数 及左边View要显示
  //比例就要设为比1小
  scale = 2 - scale;
 }
 //获取当前mainView的frame
 CGRect frame = self.mainView.frame;
  
 //重新设置mainView的frame值
 frame.size.width = frame.size.width *scale >screenW ? screenW : frame.size.width *scale;
  
 frame.size.height = frame.size.height *scale >screenH ? screenH :frame.size.height *scale;
  
 frame.origin.x += offsetX;
 frame.origin.y =(screenH - frame.size.height)*0.5;
 //返回偏移后新的frame
 return frame;
}
#define maxRightX (screenW *0.8)
#define maxLeftX (-screenW *0.6)
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
 CGFloat targetX = 0;
 //如果松手的那一下 当前mainVIew的x大于屏幕的一半
 if (self.mainView.frame.origin.x > screenW * 0.5)
 {
  //向右边定位
  targetX = maxRightX;
 }
 //如果松手的那一下 当前mainVIew的最大X值小于屏幕的一半
 else if (CGRectGetMaxX(self.mainView.frame) < screenW *0.5)
 {
  //向左边定位
  targetX = maxLeftX;
 }
  
 //计算偏移量
 CGFloat offsetX = targetX -self.mainView.frame.origin.x;
  
 self.animating = YES;
  
 [UIView animateWithDuration:0.4 animations:^{
  if (targetX == 0)
  {
   //如果targetX==0 复位
   self.mainView.frame = self.view.frame;
  }
  else
  {
   //如果targetX != 0 那就到指定位置
   self.mainView.frame = [self rectWithOffsetX:offsetX];
  }
 } completion:^(BOOL finished) {
  self.animating = NO;
 }];
   
}
@end

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

更多精彩内容其他人还在看

iOS开发笔记--详解UILabel的相关属性设置

这篇文章主要介绍了iOS开发笔记--详解UILabel的相关属性设置,对初学者具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

iOS中获取系统相册中的图片实例

这篇文章主要介绍了iOS中获取系统相册中的图片实例,具有一定的参考价值没有需要的朋友可以了解一下。
收藏 0 赞 0 分享

iOS 检测网络状态的两种方法

一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

IOS 性能优化中离屏渲染

本文主要介绍了IOS 性能优化中离屏渲染的资料,提供了几种方法讲解了优化,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

这篇文章主要介绍了iOS获取当前设备型号设备信息的总结包含iPhone7和iPhone7P,包括ios7之前之后的获取方式,本文接的非常详细,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现爆炸的粒子效果示例代码

之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

iOS毕业设计之天气预报App

这篇文章主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS轻点、触摸和手势代码开发

这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 实现多代理的方法及实例代码

这篇文章主要介绍了iOS 实现多代理的方法及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS文字渐变色效果的实现方法

在大家日常开发iOS的过程中,可能会遇到要实现文字渐变色的效果,这篇文章文章通过示例代码和详细的步骤介绍了如何利用iOS实现文字渐变色的效果,实现后的很不错,感兴趣的朋友们下面来一起看看吧。
收藏 0 赞 0 分享
查看更多