简体   繁体   English

UIScrollView中的动画

[英]Animation inside a UIScrollView

I want to fade-out a view as it is scrolling inside a parent UIScrollview. 我希望淡出视图,因为它在父UIScrollview中滚动。 When the fade-out animation begins, the scroll view stops scrolling. 淡出动画开始时,滚动视图停止滚动。 It jumps to the correct position when the fade is complete. 当淡入淡出完成时,它会跳转到正确的位置。

My fade-out is achieved with animateWithDuration and block objects, triggered upon a page-change I detect in scrollViewWillBeginDragging. 我的淡出是通过animateWithDuration和块对象实现的,在scrollViewWillBeginDragging中检测到页面更改时触发。

Does anyone know how to make them both happen simultaneously? 有谁知道如何使它们同时发生? Just to be clear, I am not 'animating' the UIScrollView scrolling - rather it is happening via user interaction of swiping. 为了清楚起见,我没有“动画化”UIScrollView滚动 - 而是通过用户交互滑动来实现。

EDIT: 编辑:

Here is the code I'm using to fade the UIView. 这是我用来淡化UIView的代码。 This code is in a UIViewController derived class, which is the delegate for a UIScrollView. 此代码位于UIViewController派生类中,该类是UIScrollView的委托。 When the user starts dragging his finger, I want to fade out the subView. 当用户开始拖动他的手指时,我想淡出subView。 But when the user starts draggin a finger, the subview fades and the scrolling stops. 但是当用户开始拖动手指时,子视图会淡化并且滚动停止。 After the subView has completely faded out, the the scroll view will then snap to the location where the user's finger is. 在subView完全淡出后,滚动视图将捕捉到用户手指所在的位置。

-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    [UIView animateWithDuration:0.5
        animations:^
        {
            self.subView.alpha = 0.0f;
        }
        completion:^(BOOL finished) { }];
}

A little late, but if you want to keep using blocks, you can use: 有点晚了,但是如果你想继续使用块,你可以使用:

animateWithDuration:delay:options:animation:complete: animateWithDuration:延迟:选项:动画:完成:

add "UIViewAnimationOptionAllowUserInteraction" to options to allow interaction while scrolling. 将“UIViewAnimationOptionAllowUserInteraction”添加到选项以允许滚动时进行交互。

I'm sure that you will still have the lag problem. 我相信你仍然会遇到滞后问题。 Here's the best way I can explain it. 这是我能解释的最佳方式。 Please forgive me in advance since I'm probably using the wrong terms. 因为我可能使用错误的条款,请提前原谅我。 All animations must run on the main thread. 所有动画都必须在主线程上运行。 When you call an animation, iOS first * P *rocesses then it * R *enders before it generates * F *rames. 当您调用动画时,iOS首先* P * rocesses然后它* R *在生成* F * rames之前进行。 It looks like this. 看起来像这样。

PPPPRRRRFFFFFFFFFFFFFFFFFF PPPPRRRRFFFFFFFFFFFFFFFFFF

But since ScrollViews don't know how long your animation is going to be or when it will end, it has to perform the animation like this. 但是由于ScrollViews不知道你的动画将会持续多长时间或者什么时候结束,它必须像这样执行动画。

PRFPRFPRFPRFPRFPRFPRFPRF PRFPRFPRFPRFPRFPRFPRFPRF

My theory is that the lag you are experiencing has to do with these two calls colliding on the main thread at the same time. 我的理论是你遇到的延迟与这两个调用同时在主线程上发生冲突有关。 I'm not sure how you would solve this problem other than with a faster chip. 除了使用更快的芯片,我不确定如何解决这个问题。 I've that you could push one animation to the CPU and one to the GPU, but I'm not that advanced at programming yet. 我认为你可以将一个动画推送到CPU,一个推动到GPU,但我还没有那么先进的编程。

very interesting ... I've checked this out, and yes, i have the same effect ... Well, it seems that the animateWithDuration somehow blocks the main thread ... which is not logical, and the documentation doesn't say anything about it either .. However there is an easy workaround, something similar to this: (i've set the animation duration to 3 so i can see that it's working while i'm moving my scroll view :) ...) 非常有趣......我已经检查了这个,是的,我有同样的效果......好吧,似乎animateWithDuration以某种方式阻止了主线程......这是不合逻辑的,文档没有说关于它的任何事情..但是有一个简单的解决方法,类似于这个:(我已经将动画持续时间设置为3,所以我可以看到它正在工作,而我正在移动我的滚动视图:) ...)

[UIView beginAnimations:@"FadeAnimations" context:nil];
[UIView setAnimationDuration:3]; 

self.subview.alpha = 0.0f;

[UIView commitAnimations];

I would suggest, since the opacity is based on the user's finger's movements in the UIScrollView, using the delegate method scrollViewDidScroll:. 我建议,因为不透明度是基于用户手指在UIScrollView中的移动,使用委托方法scrollViewDidScroll:。 The scrollView passed as a parameter can be used to check the contentOffset which is simply a CGPoint indicating how far into the content view of the UIScrollView the user has scrolled. 作为参数传递的scrollView可用于检查contentOffset,它只是一个CGPoint,指示用户滚动的UIScrollView的内容视图有多远。 Something like this can be used to relate the scroll position to the opacity of a given view in a paginated UIScrollView: 这样的东西可用于将滚动位置与分页UIScrollView中给定视图的不透明度相关联:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  // The case where I used this, the x-coordinate was relevant. You may be concerned with the y-coordinate--I'm not sure
  CGFloat percent = ((int)(scrollView.contentOffset.x) % (int)(scrollView.frame.size.width)) / scrollView.frame.size.width;
  if (percent > 0.0 && percent < 1.0) { // Of course, you can specify your own range of alpha values
    relevantView.alpha = percent; // You could also create a mathematical function that maps contentOffset to opacity in a different way than this
  }
}

According to information that is still not supposed to be widely released, all iOS 4.x versions completely block user interaction while the animation is in progress. 根据仍然不应该被广泛发布的信息,所有iOS 4.x版本在动画进行过程中完全阻止用户交互。

Isn't it interesting, though, that you're UITouches are obviously still registered during the animation? 不过有趣的是,你是UITouches显然仍然在动画中注册? Hmm... maybe that HINTS that something NEW is coming in a yet-to-be-released version! 嗯...也许这表明还有一些新东西正在发布尚未发布的版本!

Ie, If you can, read the iOS 5 Beta documentation on UIView class methods. 即,如果可以,请阅读有关UIView类方法的iOS 5 Beta文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM