【发布时间】:2014-05-27 16:05:13
【问题描述】:
我想创建一个用于锁定和解锁屏幕的滑动模式(在不松开手指的情况下滑动)。如何使用 UISwipeGestureRecognizer 做到这一点。我想保存它并在我再次尝试登录时匹配它。我怎样才能保存它?作为图像或其他东西?请帮我解决这个问题。 谢谢。
【问题讨论】:
标签: ios objective-c ios7 pattern-matching uigesturerecognizer
我想创建一个用于锁定和解锁屏幕的滑动模式(在不松开手指的情况下滑动)。如何使用 UISwipeGestureRecognizer 做到这一点。我想保存它并在我再次尝试登录时匹配它。我怎样才能保存它?作为图像或其他东西?请帮我解决这个问题。 谢谢。
【问题讨论】:
标签: ios objective-c ios7 pattern-matching uigesturerecognizer
【讨论】:
你说的“模式”是什么意思,你到底想保存什么?
您应该使用UIPanGestureRecognizer,因为滑动只是一种快速翻转手势,而平移是一种受控运动,您可以跟踪整个距离。
这是我的处理方式,从右向左移动(与锁定屏幕相反):
- (IBAction)slide:(UIPanGestureRecognizer *)gesture
{
CGPoint translate = [gesture translationInView:gesture.view];
translate.y = 0.0; // Just doing horizontal panning
if (gesture.state == UIGestureRecognizerStateChanged)
{
// Sliding left only
if (translate.x < 0.0)
{
self.slideLane.frame = [self frameForSliderLabelWithTranslate:translate];
}
}
else if ([gesture stoppedPanning])
{
if (translate.x < 0.0 && translate.x < -(gesture.view.frame.size.width / 2.5))
{
// Panned enough distance to unlock
}
else
{
// Movement was too short, put it back again
NSTimeInterval actualDuration = [self animationDuration:0.25 forWidth:gesture.view.frame.size.width withTranslation:(-translate.x)];
[UIView animateWithDuration:actualDuration delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.slideLane.frame = [self frameForSliderLabelWithTranslate:CGPointZero];
} completion:^(BOOL finished) {
}];
}
}
}
- (CGRect)frameForSliderLabelWithTranslate:(CGPoint)translate
{
return CGRectMake(translate.x, self.slideLane.frame.origin.y, self.slideLane.bounds.size.width, self.slideLane.bounds.size.height);
}
因为我不在乎手势停止的原因,所以我添加了这个类别以使 else-if 子句更具可读性。但它是可选的:
@implementation UIPanGestureRecognizer (Stopped)
- (BOOL)stoppedPanning
{
return ( self.state == UIGestureRecognizerStateCancelled
|| self.state == UIGestureRecognizerStateEnded
|| self.state == UIGestureRecognizerStateFailed);
}
@end
理想情况下,您应该考虑运动的速度,因为快速朝正确的方向轻弹就足够了。在我的例子中,我希望用户移动方块,无论多快。
【讨论】: