IOS开发之——事件处理-抽屉效果(70)

UITouch *touch=[touches anyObject];

//获取当前点

CGPoint currentPoint=[touch locationInView:self.view];

//获取上一个点

CGPoint prePoint=[touch precisePreviousLocationInView:self.view];

//x轴偏移量

CGFloat offsetX=currentPoint.x-prePoint.x;

//获取主视图的frame

CGRect frame=_mainView.frame;

frame.origin.x+=offsetX;

_mainView.frame=frame;

}

3.3 监听_mainView的frame的改变

[_mainView addObserver:self forKeyPath:@“frame” options:NSKeyValueObservingOptionNew context:nil];

  • (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)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;//显示左边

}

}

四 事件处理-抽屉效果——缩放处理


4.1 效果

4.2 原理

假设:移动的x距离为320,y距离为50

  • offsetY=offsetX*50/320

  • scale=currentH/screenH

  • currentH=screenH-2*offsetY

  • x=frame.origin.x+offsetX

  • h=frame.size.height*scale

  • w=frame.size.weight*scale

  • y=(screenH-h)*0.5

4.3 滑动时处理

获取当前视图view
  • (CGRect)getCurrentFrameWithOffsetX:(CGFloat)offsetX

{

CGFloat screenW=[UIScreen mainScreen].bounds.size.width;

CGFloat screenH=[UIScreen mainScreen].bounds.size.height;

//获取y轴偏移量,手指每移动一点,y轴偏移多少

CGFloat offsetY=offsetX*MaxY/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;

}

滑动时重新赋值视图

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//获取UITouch对象

UITouch *touch=[touches anyObject];

//获取当前点

CGPoint currentPoint=[touch locationInView:self.view];

//获取上一个点

CGPoint prePoint=[touch precisePreviousLocationInView:self.view];

//x轴偏移量

CGFloat offsetX=currentPoint.x-prePoint.x;

//获取主视图的frame

CGRect frame=_mainView.frame;

frame.origin.x+=offsetX;

_mainView.frame=[self getCurrentFrameWithOffsetX:offsetX];

}

五 事件处理-抽屉效果——滑动定位


5.1 说明

  • 向右滑动时:当主视图的最大x大于屏幕的一半时,自动定位到屏幕右边

  • 向左滑动时:当主视图的最大x小于屏幕的一半,自动定位到屏幕左边

5.2 代码实现

//定位

  • (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

CGFloat target=0;

CGFloat screenW=[UIScreen mainScreen].bounds.size.width;

if (_mainView.frame.origin.x>screenW*0.5) { //定位到右边

target=rightTarget;

}else if(CGRectGetMaxX(_mainView.frame)<screenW*0.5) //定位到左边

{

target=leftTarget;

}

[UIView animateWithDuration:0.25 animations:^{

if (target) { //定位到左或右

//获取偏移量

CGFloat offsetX=target-_mainView.frame.origin.x;

_mainView.frame=[self getCurrentFrameWithOffsetX:offsetX];

}else{ //还原

_mainView.frame=self.view.bounds;

}

}];

}

六 事件处理-抽屉效果——定位视图复


6.1 说明

  • 当向左向右滑动至定位点时,点击视图重新复位到原来位置

6.2 代码实现

定义变量

@property(nonatomic,assign) BOOL isDraging;

isDraging何时为YES

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

_isDraging=YES;

}

总结

本文讲解了我对Android开发现状的一些看法,也许有些人会觉得我的观点不对,但我认为没有绝对的对与错,一切交给时间去证明吧!愿与各位坚守的同胞们互相学习,共同进步!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
Draging何时为YES

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

_isDraging=YES;

}

总结

本文讲解了我对Android开发现状的一些看法,也许有些人会觉得我的观点不对,但我认为没有绝对的对与错,一切交给时间去证明吧!愿与各位坚守的同胞们互相学习,共同进步!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值