- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现 View 的拖动,当拖动停止时, View 滑到顶部并消失。我可以使用 UIPanGestureRecognizer 很好地拖动 View 。但在那之后,我用动画让它滑出。问题是在拖动之后,在 View 移动之前总是会有一点小问题......我四处搜索,无法弄清楚如何解决这个问题。这是代码:
- (void)moveView2:(UIPanGestureRecognizer *)pan {
CGPoint delta = [pan translationInView:self.view];
CGPoint newCenter = CGPointMake(_view2.center.x, _view2.center.y + delta.y);
_view2.center = newCenter;
[pan setTranslation:CGPointZero inView:self.view];
if (pan.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [pan velocityInView:self.view];
NSLog(@"Velocity is %f, %f", velocity.x, velocity.y);
// Here is the delay
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.5f initialSpringVelocity:500 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_view2.center = CGPointMake(_view2.center.x, -600);
} completion:nil];
}
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *fbButton;
@property (weak, nonatomic) IBOutlet UIButton *twitterButton;
@property UIView *view1;
@property UIView *view2;
@property CGPoint startPoint;
@property CGPoint endPoint;
@property CGPoint originCenter;
@property double startTime;
@property double endTime;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = [UIScreen mainScreen].bounds;
_view1 = [[UIView alloc] initWithFrame:frame];
_view1.backgroundColor = [UIColor redColor];
[self.view addSubview:_view1];
_view2 = [[UIView alloc] initWithFrame:frame];
_view2.backgroundColor = [UIColor greenColor];
[self.view addSubview:_view2];
UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveView2:)];
[_view2 addGestureRecognizer:pan1];
/*
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUpDown:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionUp];
[_view2 addGestureRecognizer:swipe];
*/
}
/*
- (void)swipeUpDown:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"Swipe up");
[UIView animateWithDuration:0.5 animations:^{
_view2.center = CGPointMake(_view2.center.x, -600);
}];
}
}*/
- (void)moveView2:(UIPanGestureRecognizer *)pan {
CGPoint bigViewDelta = [pan translationInView:self.view];
CGPoint newCenter = CGPointMake(_view2.center.x, _view2.center.y + bigViewDelta.y);
_view2.center = newCenter;
[pan setTranslation:CGPointZero inView:self.view];
if (pan.state == UIGestureRecognizerStateEnded) {
CGPoint velocityOfPan = [pan velocityInView:self.view];
CGFloat velocityOfPanAbsolute = sqrt(velocityOfPan.x * velocityOfPan.x + velocityOfPan.y * velocityOfPan.y);
// get simple points per second
CGPoint currentPoint = _view2.center;
CGPoint finalPoint = CGPointMake(_view2.center.x, -600);
CGFloat distance = sqrt((finalPoint.x - currentPoint.x) * (finalPoint.x - currentPoint.x) + (finalPoint.y - currentPoint.y) * (finalPoint.y - currentPoint.y));
// how far to travel
CGFloat duration = 0.5;
CGFloat animationVelocity = velocityOfPanAbsolute / (distance / duration);
[UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:animationVelocity options:0 animations:^{
_view2.center = CGPointMake(_view2.center.x, -600);
} completion:nil]; }
}
- (void)viewDidAppear:(BOOL)animated {
/*
[UIView transitionFromView:_view2 toView:_view1 duration:2 options:UIViewAnimationOptionCurveEaseInOut completion:^(BOOL finished) {
}];
*/
/*
// get the view that's currently showing
UIView *currentView = _view2;
// get the the underlying UIWindow, or the view containing the current view view
UIView *theWindow = [currentView superview];
// remove the current view and replace with myView1
[currentView removeFromSuperview];
//[theWindow addSubview:newView];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最佳答案
你的意思是动画发生之前的延迟?嗯,我没有看到任何会导致这种情况的东西,但是 animateWithDuration
的参数非常非常好奇。
UIViewAnimationOptionCurveEaseInOut
对我来说没有意义,因为这常用于说“慢慢开始,加快速度,也慢慢停止”。这对于一个标准动画来说很好,您不希望动画的开始过于刺耳。但这在这种情况下没有意义,因为您可能希望它不要缓慢启动,而是使用您最初提供的任何速度。如果我使用任何 option
, 我会使用 UIViewAnimationOptionCurveEaseOut
.即使这样也没有意义,因为它是 usingSpringWithDamping
指示在此动画结束时发生的情况的参数。 (而且,最重要的是,您将其拖出屏幕,所以我不清楚您为什么关心退出曲线。)initialSpringVelocity
对我来说似乎也不对。正如此参数的文档所述:The initial spring velocity. For smooth start to the animation, match this value to the view’s velocity as it was prior to attachment.
A value of
1
corresponds to the total animation distance traversed in one second. For example, if the total animation distance is 200 points and you want the start of the animation to match a view velocity of 100 pt/s, use a value of0.5
.
500
表示您想在一秒钟内从当前位置到最终位置的距离旅行 500 倍。这快得离谱。usingSpringWithDamping
的 0.5
.那是有弹性的。为什么你要让它在你正在使用的所有东西中弹出,因为它会停止在屏幕上并且你无论如何都看不到它?令我震惊的是,任何小于 1.0
的值在一个或多个初始弹跳期间, View 可能会弹回可见屏幕。我不会使用低于 1.0
的任何东西如果在屏幕外设置动画。 CGPoint velocityOfPan = [pan velocityInView:self.view];
CGFloat velocityOfPanAbsolute = hypof(velocityOfPan.x, velocityOfPan.y); // get simple points per second
CGPoint currentPoint = _view2.center.x;
CGPoint finalPoint = CGPointMake(_view2.center.x, -600);
CGFloat distance = hypof(finalPoint.x - currentPoint.x, finalPoint.y - currentPoint.y); // how far to travel
CGFloat duration = 0.5;
CGFloat animationVelocity = velocityOfPanAbsolute / (distance / duration);
[UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:animationVelocity options:0 animations:^{
_view2.center = CGPointMake(_view2.center.x, -600);
} completion:nil];
animateWithDuration
被“缓入缓出”和戏剧性的
initialSpringVelocity
组合弄糊涂了,我实际上不明白为什么会导致“打嗝”。
UILongPressGestureRecognizee
来解决这个问题。与
minimumPressDuration
为零(尽管您必须自己计算
translationInView
)。
关于ios - UIPanGestureRecognizer 状态结束后动画中的小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26623463/
UIGestureRecognizer 的文档引用重置函数。 但是,在 UIPanGestureRecognizer(UIGestureRecognizer 的子级)上调用 reset() 会生成此错
我有一个 UIPanGestureRecognizer,它根据我的喜好检测了太多次。我需要一个计数器并计算 3 个已完成的手势。每次我用手指平移识别器位置时,它都会触发 3 到 4 次。甚至触发了5个
我正在使用 UIPanGestureRecogniser实现拖放。当拖动开始时,我需要识别被拖动的对象。但是对象相对较小。如果用户没有在对象的中心点击对象,它就不会被拖动。 问题是当手势处理程序第一次
我正在尝试使用UIPanGestureRecognizer实现页面翻转动画。这是代码: - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
我有一个 UIView 子类,我在其中添加了一个 UIPanGestureRocognizer: _panGestureRecognizer = [[UIPanGestureRecognizer al
我试图使用 UIPanGestureRecognizer 来移动我的对象,但我似乎无法让它顺利移动。当我朝一个特定的方向移动并转向相反的方向时,它不会立即使用react。我认为该值可能有问题,因为它在
我正在尝试实现 View 的拖动,当拖动停止时, View 滑到顶部并消失。我可以使用 UIPanGestureRecognizer 很好地拖动 View 。但在那之后,我用动画让它滑出。问题是在拖动
我在touchesEnded方法中调用了一个名为addProjectile的方法。 addProjectile接收touchesEnded方法接收的触摸的NSSet。为简单起见,我仅将相关代码发布到我
我遇到了 UIPanGestureRecognizer 的问题。假设我使用不同的标签动态添加 10 个按钮,当我添加第一个按钮时,尝试将其拖动到其他位置,然后它就可以正常工作。然后,如果我添加其他按钮
我有一个基类,它处理单元格的一些基本动画。在我的子类中,我已经覆盖了识别器目标并想做一些其他事情,但翻译始终是 0 因为我在基类中调用翻译被重置为 .zero . 有没有办法让我的基类执行基本动画并使
我正在使用 UIPanGestureRecognizer 将图像拖出 UICollectionViewCell,然后查看它是否与屏幕上其他位置的另一个图像发生碰撞。为此,我需要能够匹配他们的翻译坐标。
我创建了一个UIPanGestureRecognizer,但问题是当我将 View 向右滑动时, View 会向左移动。 当我向左滑动 View 时,它会向右滑动。为什么? let panGestur
我正在开发一个类似于 Tinder 中的卡片 View 。当卡片X原点大于我声明的值时,它会移出屏幕。否则,它会再次粘在中心。我正在 UIPanGestureRecognizer 函数中执行所有这些操
我有一个 UIPanGestureRecognizer 附加到父 View ,带有各种 CCSprite 我想在平移时在父 View 中移动。使用 [gesture locationOfTouch:i
使用 UIPanGestureRecognizer 我们正在移动 UICollectionView 中的单元格 当我们开始拖动时 UIGestureRecognizerStateBegan 被调用。但
我有 6 个 UIImageView,每个都连接到 UIPanGestureRecognizer,它们都连接到相同的方法。方法是: - (IBAction)handlePan:(UIPanGestur
我是 swift 和 xcode 的新手。使用 xcode 10 并尝试制作一个类似应用程序的嗡嗡声,我需要在 View 中移动多个图像。我发现了很多关于 UIPanGestureRecognizer
我有一个侧面板,其中有一个 UIImageView,我在其中附加了一个 UIPanGestureRecognizer,以便您可以推/拉侧边栏。效果很好。 问题是我有一些按钮有时会出现在侧边栏下方。如果
本质上,我想做的是移动 View 以跟随用户的平移。只要使用相同的平移对象,这种方法就可以正常工作。当用户释放并启动另一个平移时,问题就出现了。 根据文档,translationInView 中的值是
我正在将 UIPanGestureRecognizer 添加到我的 View Controller 中的一个自定义 View 。 我还使用 MFSideMenu 作为滑动菜单,在其框架中将 UIPan
我是一名优秀的程序员,十分优秀!