//定义属性宏
#define LWKeyPath(objc, keyPath) @(((void)objc.keyPath, #keyPath))
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
#define MaxY100
#define targetR300
#define targetL -220
@interface ViewController()
@property(nonatomic,weak)UIView*mainV;
@property(nonatomic,weak)UIView*leftV;
@property(nonatomic,weak)UIView*rightV;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//添加控件
[selfsetUpAllViews];
//添加手势
[selfsetGesture];
//添加点控手势
[selfsetTapGesture];
//添加监听frame移动方向的kvo
// KVO作用:时刻监听某个对象的某个属性的改变
// _main frame属性的改变
// Observer:观察者
// KeyPath:监听的属性
[_mainVaddObserver:selfforKeyPath:LWKeyPath(_mainV,frame)options:NSKeyValueObservingOptionNewcontext:nil];
}
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context{
if(_mainV.frame.origin.x>0) {
_rightV.hidden=YES;
}elseif(_mainV.frame.origin.x<0){
_rightV.hidden=NO;
}
}
-(void)dealloc{
[_mainVremoveObserver:selfforKeyPath:LWKeyPath(_mainV,frame)];
}
//点控手势
- (void)setTapGesture{
//新建点控手势识别
UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap)];
[self.viewaddGestureRecognizer:tap];
}
//点控触发事件
- (void)tap{
[UIViewanimateWithDuration:0.25animations:^{
_mainV.frame= [UIScreenmainScreen].bounds;
}];
}
//添加手势
- (void)setGesture{
//拖拽手势
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panGestureRecognizer:)];
//添加到控件上
[_mainVaddGestureRecognizer:pan];
}
//触发拖拽事件
- (void)panGestureRecognizer: (UIPanGestureRecognizer*)panGR{
//获取手势的偏移量
CGPointtransP = [panGRtranslationInView:_mainV];
//获取x轴的偏移量,相对于上一次
CGFloatoffsetX = transP.x;
//平移
// _mainV.transform = CGAffineTransformTranslate(_mainV.transform, offsetX, 0);
// //要想实现相对上个位置的移动,就要实现复位
// //复位
// [panGR setTranslation:CGPointZero inView:_mainV];
//使用frame同时实现压缩
_mainV.frame= [selfframeWithOffsetX:offsetX];
//手势操作时,实现相对效果,就要复位
[panGRsetTranslation:CGPointZeroinView:_mainV];
//如果拖拽手势结束
if(panGR.state==UIGestureRecognizerStateEnded) {
CGFloattarget =0;
//判断当前的frame
if(_mainV.frame.origin.x>screenW*0.5) {
//右移一半以上
target =targetR;
}elseif(CGRectGetMaxX(_mainV.frame)
//左移一半以上
target =targetL;
}
//获得x的偏移量
offsetX = target -_mainV.frame.origin.x;
//动画跳转新的frame
[UIViewanimateWithDuration:0.25animations:^{
_mainV.frame= [selfframeWithOffsetX:offsetX];
}];
}
}
- (CGRect)frameWithOffsetX: (CGFloat)offsetX{
//获取当前main的frame
CGRectframe =_mainV.frame;
//计算当前的x,y,w,h
CGFloatx = frame.origin.x+ offsetX;
//去x的绝对值,排除掉左移,y值变大的效果
CGFloattem_x =fabs(x);
//获取最新的y等比例缩放
CGFloaty = tem_x/screenW*MaxY;
//获取最新的高度
CGFloath =screenH-2* y;
//获取最新的缩放值
CGFloatscale = h/screenH;
//获取最新的宽度
CGFloatw =screenW* scale;
returnCGRectMake(x, y, w, h);
}
//添加控件
- (void)setUpAllViews{
//红色
UIView*v1 = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
v1.backgroundColor= [UIColorredColor];
[self.viewaddSubview:v1];
_leftV= v1;
//蓝色
UIView*v2 = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
v2.backgroundColor= [UIColorblueColor];
[self.viewaddSubview:v2];
_rightV= v2;
//黄色
UIView*v3 = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
v3.backgroundColor= [UIColoryellowColor];
[self.viewaddSubview:v3];
_mainV= v3;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论