AJ分享,必须精品

先看效果

实现过程

第一步,把三个view设置好,还有颜色

#warning 第一步
- (void)addChildView
{
// left
UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
leftView.backgroundColor = [UIColor greenColor];
[self.view addSubview:leftView];
_leftView = leftView; // right
UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
rightView.backgroundColor = [UIColor blueColor];
[self.view addSubview:rightView];
_rightView = rightView; // mainView
UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
mainView.backgroundColor = [UIColor redColor];
[self.view addSubview:mainView];
_mainView = mainView;
}

第二步,获取点,然后得到手指滑动偏移量的x值,然后根据算法算出需要做出的位移。

#warning 第二步
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取UITouch对象
UITouch *touch = [touches anyObject]; // 获取当前点
CGPoint currentPoint = [touch locationInView:self.view]; // 获取上一个点
CGPoint prePoint = [touch previousLocationInView:self.view]; // x轴偏移量:当手指移动一点的时候,x偏移多少
CGFloat offsetX = currentPoint.x - prePoint.x; // 设置当前主视图的frame
_mainView.frame = [self getCurrentFrameWithOffsetX:offsetX]; _isDraging = YES;
}

第三步:观察_mainView的frame改变

#warning 第三步 观察_mainView的frame改变
// 2.监听
/**
* 给_mainView添加一个观察者
*
* KeyPath:监听frame这个属性
*
* options:监听新值的改变
*/
[_mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]; } // 当_mainView的frame属性改变的时候就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@", NSStringFromCGRect(_mainView.frame)); if (_mainView.frame.origin.x < 0) { // 往左移动
// 显示右边
_rightView.hidden = NO;
// 隐藏左边
_leftView.hidden = YES;
}else if (_mainView.frame.origin.x > 0){ // 往右移动
// 显示左边
_rightView.hidden = YES;
// 隐藏右边
_leftView.hidden = NO; }
}

第四步

#warning 第四步:根据X轴的偏移量算出当前主视图的frame与定位复位。
#define HMMaxY 60
// 当手指偏移一点,根据X轴的偏移量算出当前主视图的frame
- (CGRect)getCurrentFrameWithOffsetX:(CGFloat)offsetX
{
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat screenH = [UIScreen mainScreen].bounds.size.height; // 获取y轴偏移量,手指每移动一点,y轴偏移多少
CGFloat offsetY = offsetX * HMMaxY / screenW; CGFloat scale = (screenH - 2 * offsetY) / screenH; if (_mainView.frame.origin.x < 0) { // 往左边滑动
scale = (screenH + 2 * offsetY) / screenH;
} // 获取之前的frame
CGRect frame = _mainView.frame;
frame.origin.x += offsetX;
frame.size.height = frame.size.height *scale;
frame.size.width = frame.size.width *scale;
frame.origin.y = (screenH - frame.size.height) * 0.5; return frame;
} #define HMRTarget 250
#define HMLTarget -220
/*
_mainView.frame.origin.x > screenW * 0.5 定位到右边
CGRectGetMaxX(_mainView.frame) < screenW * 0.5 定位到左边 -220 */
// 定位
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ // 复位
if (_isDraging == NO && _mainView.frame.origin.x != 0) {
[UIView animateWithDuration:0.25 animations:^{ _mainView.frame = self.view.bounds;
}];
} CGFloat screenW = [UIScreen mainScreen].bounds.size.width; CGFloat target = 0;
if (_mainView.frame.origin.x > screenW * 0.5) { // 定位到右边
target = HMRTarget;
}else if (CGRectGetMaxX(_mainView.frame) < screenW * 0.5) { // 定位到左边
target = HMLTarget;
} [UIView animateWithDuration:0.25 animations:^{ if (target) { // 在需要定位左边或者右边 // 获取x轴偏移量
CGFloat offsetX = target - _mainView.frame.origin.x; // 设置当前主视图的frame
_mainView.frame = [self getCurrentFrameWithOffsetX:offsetX]; }else{ // 还原
_mainView.frame = self.view.bounds;
}
}]; _isDraging = NO; }

最新文章

  1. Java多线程系列--“JUC锁”10之 CyclicBarrier原理和示例
  2. Codeforces Round #262 (Div. 2) 1004
  3. 配置DelegatingFilterProxy使用Spring管理filter chain
  4. 什么是java path环境变量
  5. Squid的简单使用
  6. Struts2知多少(1) Struts2 MVC架构
  7. Pointcut is not well-formed: expecting &#39;identifier&#39; at character position 0
  8. Ubuntu 16.04 LTS Final Beta about JAVA
  9. va_list/va_start/va_arg/va_end深入分析【转】
  10. Android Gson深入分析
  11. Drools 6.5 Final 入门
  12. JavaScript 数组最大值
  13. 代码调用t.cn接口生成短址
  14. 如何写java求和源代码
  15. Python学习笔记六:数据库操作
  16. AssetBundle加载API
  17. linux进程概论
  18. Fast Walsh-Hadamard Transform——快速沃尔什变换(二)
  19. 初识tornado
  20. Python 使用 UTF-8 编码(转)

热门文章

  1. RTSP协议进行视频取流的方法、注意点及python实现
  2. cmdb 配置
  3. CF1327A Sum of Odd Integers 题解
  4. html5 window.postMessage 传递数据的使用
  5. vue基础指令学习
  6. 移动深度学习 Mobile-deep-learning(MDL)
  7. 使用FME平移shapefile文件
  8. redis的使用及配置
  9. Math对象常用方法介绍
  10. php _weakup()反序列化漏洞