最后是最重要的,手势拖动的实现方法
- (void)moveViewWithGesture:(UIPanGestureRecognizer *)panGes
{
static CGFloat currentTranslateX;
if (panGes.state == UIGestureRecognizerStateBegan)
{
currentTranslateX = _mainContentView.transform.tx;
}
if (panGes.state == UIGestureRecognizerStateChanged)
{
CGFloat transX = [panGes translationInView:_mainContentView].x;
transX = transX + currentTranslateX;
CGFloat sca;
if (transX > 0)
{
[self.view sendSubviewToBack:_rightSideView];
[self configureViewShadowWithDirection:RMoveDirectionRight];
if (_mainContentView.frame.origin.x < RContentOffset)
{
sca = 1 - (_mainContentView.frame.origin.x/RContentOffset) * (1-RContentScale);
}
else
{
sca = RContentScale;
}
}
else //transX < 0
{
[self.view sendSubviewToBack:_leftSideView];
[self configureViewShadowWithDirection:RMoveDirectionLeft];
if (_mainContentView.frame.origin.x > -RContentOffset)
{
sca = 1 - (-_mainContentView.frame.origin.x/RContentOffset) * (1-RContentScale);
}
else
{
sca = RContentScale;
}
}
CGAffineTransform transS = CGAffineTransformMakeScale(1.0, sca);
CGAffineTransform transT = CGAffineTransformMakeTranslation(transX, 0);
CGAffineTransform conT = CGAffineTransformConcat(transT, transS);
_mainContentView.transform = conT;
}
else if (panGes.state == UIGestureRecognizerStateEnded)
{
CGFloat panX = [panGes translationInView:_mainContentView].x;
CGFloat finalX = currentTranslateX + panX;
if (finalX > RJudgeOffset)
{
CGAffineTransform conT = [self transformWithDirection:RMoveDirectionRight];
[UIView beginAnimations:nil context:nil];
_mainContentView.transform = conT;
[UIView commitAnimations];
_tapGestureRec.enabled = YES;
return;
}
if (finalX < -RJudgeOffset)
{
CGAffineTransform conT = [self transformWithDirection:RMoveDirectionLeft];
[UIView beginAnimations:nil context:nil];
_mainContentView.transform = conT;
[UIView commitAnimations];
_tapGestureRec.enabled = YES;
return;
}
else
{
CGAffineTransform oriT = CGAffineTransformIdentity;
[UIView beginAnimations:nil context:nil];
_mainContentView.transform = oriT;
[UIView commitAnimations];
_tapGestureRec.enabled = NO;
}
}
}
根据不同的状态设置不同状态下的仿射变换值,来赋给_mainContentView,在到达临界状态后,该视图的缩放不再进行。
当拖动手势结束后,根据拖动的位置,来确定视图如何进行移动。
这个例子主要的代码都贴在了上面,这里的素材是随便取的。现在的网易客户端的侧栏背景为深色视图,左侧栏栏目跟这个demo差不多。