文章搬運來源:blog.csdn.net/Calvin_zhou…面試
做者:PGzxcmarkdown
對iOS開發感興趣,能夠看一下做者的iOS交流羣:812157648,你們能夠在裏面吹水、交流相關方面的知識,羣裏還有我整理的有關於面試的一些資料,歡迎你們加羣,你們一塊兒開車oop
- (void)addSubView
{
//left view
UIView *leftView=[[UIView alloc]initWithFrame:self.view.bounds];
leftView.backgroundColor=[UIColor greenColor];
[self.view addSubview:leftView ];
_leftView=leftView;
//right view
UIView *rightView=[[UIView alloc]initWithFrame:self.view.bounds];
rightView.backgroundColor=[UIColor blueColor];
[self.view addSubview:rightView];
_rightView=rightView;
//main view
UIView *mainView=[[UIView alloc]initWithFrame:self.view.bounds];
mainView.backgroundColor=[UIColor redColor];
[self.view addSubview:mainView];
_mainView=mainView;
}
複製代碼
-(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=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;//顯示左邊
}
}
複製代碼
假設:移動的x距離爲320,y距離爲50佈局
- (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];
}
複製代碼
//定位
- (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;
}
}];
}
複製代碼
@property(nonatomic,assign) BOOL isDraging;
複製代碼
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_isDraging=YES;
}
複製代碼
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_isDraging=NO;
}
複製代碼
if (_isDraging==NO&&_mainView.frame.origin.x!=0) {
[UIView animateWithDuration:0.25 animations:^{
_mainView.frame=self.view.bounds;
}];
}
複製代碼