我想创建一个用于锁定和解锁屏幕的滑动模式(在不取下手指的情况下滑动)。我怎么能用UISwipeGestureRecognizer做到这一点。我想保存它并在我再次尝试登录时匹配它。我怎么能保存?作为图像或其他什么?请帮我这个。 谢谢。
答案 0 :(得分:1)
答案 1 :(得分:0)
当你谈论"模式"时,你的意思是什么?你想要保存什么?
你应该使用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
理想情况下,您需要考虑运动的速度,因为快速向正确的方向轻弹就足够了。在我的情况下,无论多快,我都希望用户移动块。