我想做平滑的滑动动画。我只想在用户仅从右边框或左边框滑动页面时才能进行滑动。页面中间不应该滑动。滑动应该可以从左到右,从右到左。
我尝试了很多滑动动画示例代码或演示代码。但它不是我想要的。我想要这样的动画https://itunes.apple.com/in/app/clear-tasks-to-do-list/id493136154?mt=8
在这个应用程序中它就像我们触摸右边框时一样顺畅地滑动。请引导我做这个动画。提前致谢。
答案 0 :(得分:2)
很抱歉迟到的回复。刚看到这个问题。
如果您希望从边缘进行滑动操作,请在主视图的远端(左侧和右侧)创建2个子视图,然后给出30或40的宽度。
我相信你还有其他两个视图从左到右弹出。所以为了做到这一点,你需要在主视图的顶部添加2个视图。 现在对于左视图,将连接到主视图的右侧horizondal空间约束设置为小于(-1)x主视图宽度的值。对于右视图,将其正确的horizondal空间约束连接到主视图,使其值大于主视图的宽度,以便两个视图都在主视图之外
X代表大于或等于主视图宽度的值
添加两个NSLayoutConstraint变量作为IBOutlet,保存这两个值。
NSLayoutConstraint *leftViewHorizondalRightPadding;
NSLayoutConstraint *rightViewHorizondalRightPadding;
现在将UISwipeGestures添加到这些子视图中(以橙色表示)。
UISwipeGestureRecognizer *leftToRightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[leftToRightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.leftSubview addGestureRecognizer:leftToRightSwipe];
UISwipeGestureRecognizer *rightToLeftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[rightToLeftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.rightSubview addGestureRecognizer:rightToLeftSwipe];
///Now in the swipe handler distinguish the swipe actions
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
//It's leftToRight
leftViewHorizondalRightPadding.constant = 0;
[UIView animateWithDuration:1
animations:^{
[self.view layoutIfNeeded];
}];
}
else {
//It's rightToLeft
rightViewHorizondalRightPadding.constant = 0;
[UIView animateWithDuration:1
animations:^{
[self.view layoutIfNeeded];
}];
}
}
}
这将从左到右,从右到左进行滑动动画。
希望这会有所帮助..
答案 1 :(得分:0)
创建2个滑动手势识别器后,您应该设置其代理。然后使用此委托方法:
UISwipeGestureRecognizer *_swipeLeft;
UISwipeGestureRecognizer *_swipeRight;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
static const CGFloat borderWidth = 50.0f;
if(gestureRecognizer == _swipeLeft) {
return [gestureRecognizer locationInView:self].x > self.frame.size.width - borderWidth;
}
else if(gestureRecognizer == _swipeRight) {
return [gestureRecognizer locationInView:self].x < borderWidth;
}
return YES;
}
请注意,对于平滑滑动/拖动,您可能需要使用平移手势或甚至长按手势识别器,而不是滑动手势。它们非常相似,只是长按需要一点时间才能开始(可设置)。如果您使用它们,您可能仍希望使用相同的委托方法。或者您可以简单地在手势目标方法中执行所有代码。尝试这样的事情:
CGPoint gestureStartPoint;
- (void)dragFromBoreder:(UIGestureRecognizer *)sender {
static const CGFloat borderWidth = 50.0f;
switch (sender.state) {
case UIGestureRecognizerStateBegan: {
CGPoint location = [sender locationInView:self];
if(location.x > borderWidth || location.x < self.frame.size.width-borderWidth) {
//break the gesture
sender.enabled = NO;
sender.enabled = YES;
}
else {
gestureStartPoint = location;
}
break;
}
case UIGestureRecognizerStateChanged: {
CGPoint location = [sender locationInView:self];
CGFloat deltaX = location.x - gestureStartPoint.x;
UIView *viewToMove;
CGPoint defaultCenter;
viewToMove.center = CGPointMake(defaultCenter.x+deltaX, defaultCenter.y);
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled: {
CGPoint location = [sender locationInView:self];
CGFloat deltaX = location.x - gestureStartPoint.x;
/*
if(deltaX > someWidth) {
show the left view
}
else if(deltaX < -someWidth) {
show the right view
}
else {
put everything back the way it was
}
*/
break;
}
default:
break;
}
}
答案 2 :(得分:0)
在ios7中有一个手势识别器specifically for gestures beginning from the edge of the screen。你应该用它。
我无法解决你的“顺利”问题,因为你还没有说出你当前的动画是什么样的,或者你是怎么做的。但是平移手势,例如直接更新视图位置的平移手势,将比滑动更平滑地跟踪用户的移动。