给要侧滑的View添加UIPanGestureRecognizer
#pragma mark 手势识别器回调方法
- (void)dragView:(UIPanGestureRecognizer *)gesture{
switch (gesture.state) {
case
UIGestureRecognizerStateBegan:
[self
startDrag];
break;
case
UIGestureRecognizerStateEnded:
[self
endDrag];
break;
default:
[self
Draging:gesture];
break;
}
}
#pragma mark 开始拖动,记录下开始的x值
- (void)startDrag{
CGRect frame = _mainView.frame;
_lastDownX = frame.origin.x;
}
#pragma mark 正在拖动,让View跟着动
- (void)Draging:(UIPanGestureRecognizer *)gesture{
CGFloat maxX = [UIScreen
mainScreen].bounds.size.width *
kSlideRatio;
CGPoint transform = [gesture
translationInView:_mainView];
CGRect frame = _mainView.frame;
frame.origin.x =
_lastDownX + transform.x;
if (frame.origin.x <
0) { //不可以移动到屏幕左边去
frame.origin.x =
0;
}else
if(frame.origin.x > maxX){
frame.origin.x = maxX;
//限制移动的最右边
}
_mainView.frame = frame;
//
设置界面scale动画
CGFloat tx = frame.origin.x;
if (tx < 0) {
tx =
0;
}
CGFloat txScale = tx /
_settingsViewController.view.frame.size.width;
//
让imageview缩放
double scale =
kDefaultSettingScale + txScale * (1 -
kDefaultSettingScale);
if (scale > 1){
scale =
1;
}
CGAffineTransform cgTransform =
CGAffineTransformMakeScale(scale, scale);
[_settingsViewController
setAnimationScale:cgTransform];
}
#pragma mark 拖动结束,判断要往哪边滚动
- (void)endDrag{
int maxX = [self
getSettingRight];
int currentX = _mainView.frame.origin.x;
if (currentX < maxX *
0.5) { //没到一半,回到左边
currentX =
0;
}else{
//回到右边
currentX = maxX;
}
CGRect frame = _mainView.frame;
frame.origin.x = currentX;
[self
scrollMainViewToFrame:frame withDuration:0.25];
}