ollViewDidScroll或scrollViewDidZo

ollViewDidScroll或scrollViewDidZo

本文介绍了在iOS 4.0中,为什么UIScrollView zoomToRect:animated:在动画时不触发scrollViewDidScroll或scrollViewDidZoom委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要密切监视滚动视图的比例,以便我可以根据滚动视图的动画缩放更新内容视图的元素(管理多个 CALayers 的子视图)。

I need to closely monitor the scale of the scroll view so that I can update the content view's elements (a subview managing multiple CALayers) according to the scroll view's animated zoom.

在iOS 3.1上,一切都按预期工作,我使用 zoomToRect:animated: UIScrollViewDelegate的 scrollViewDidScroll:消息 在动画发生时反复调用,让我根据实际缩放更新子视图元素。

On iOS 3.1, everything works as expected, I use zoomToRect:animated: and the scrollViewDidScroll: message of the UIScrollViewDelegate gets called repeatedly while the animation takes place, letting me update the subview elements according to actual zoom.

相同的代码iOS 4.0没有表现出相同的行为。当我调用 zoomToRect:animated:时,委托( scrollViewDidScroll: scrollViewDidZoom )只会被称为一次,使我的子元素去同步直到动画结束。实际上,子元素会立即跳转然后被缩放动画捕捉到所有内容都在正确的位置。就像动画没有在子视图 CALayers 上获取修改一样。

The same code on iOS 4.0 does not exibit the same behavior. When I call zoomToRect:animated:, the delegates (scrollViewDidScroll: and scrollViewDidZoom) only get called once, which makes my sub elements desinchronized until the animation is finished. In fact, the sub elements immediately jump and then get caught up by the zoom animation until everything is in the correct place. It's as if the animation is not picking up the modifications on the subviews CALayers.

我已尝试使用动画块手动设置动画,但情况相同,没有渐进式回调调用。我也尝试过KVO,但我不清楚如何使用UIScrollView管理的动画。

I have tried animating manually with animation blocks, but the situation is the same, no progressive callback calls. I have also tried KVO, but it is not clear to me how I would tap into a UIScrollView-managed animation.

iOS 4上是否有解决方法可以让我当 UIScrollView 比例为动画时,将比例值推送到我的子视图?

Is there a workaround on iOS 4 that would allow me to push the scale value to my subviews as the UIScrollView scale is animated?

推荐答案

残酷但有效。

定义一些属性:

@property (nonatomic, strong) UIView *zoomedView;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) CGFloat currentScale;

通知 UIScrollView 哪个 UIView 将被缩放:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
     return self.zoomedView;
}

注册 CADisplayLink 滴答声。它也会运行Core Animation,所以它会以相同的间隔启动:

Register for a CADisplayLink ticks. It runs Core Animation as well, so it will be kicked on same intervals:

self.displayLink  = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

检查zoomedView的 presentationLayer 获取当前值。

Check zoomedView's presentationLayer for current values.

- (void)displayLinkTick
{
    CALayer *zoomedLayer = self.zoomedView.layer.presentationLayer;
    CGFloat scale = zoomedLayer.transform.m11;

    if (scale != self.currentScale) {
         self.currentScale = scale; // update property

         // the scale has changed!
    }
}

这篇关于在iOS 4.0中,为什么UIScrollView zoomToRect:animated:在动画时不触发scrollViewDidScroll或scrollViewDidZoom委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:11