在 iOS 4.0 中,为什么 UIScrollView zoomToRect:animated:

时间:2022-12-28
本文介绍了在 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:UIScrollViewDelegatescrollViewDidScroll: 消息 在动画发生时重复调用,让我根据实际缩放更新子视图元素.

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)只会被调用 once,这使我的子元素去同步,直到动画完成.实际上,子元素会立即跳跃,然后被缩放动画赶上,直到一切都在正确的位置.就好像动画没有拾取子视图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 将被缩放:

Inform UIScrollView which UIView is going to be zoomed:

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

注册一个 CADisplayLink 滴答声.它也运行核心动画,所以它会以相同的间隔被踢:

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 委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:每次缩放后重绘 UIScrollView 内容 下一篇:_UIQueuingScrollView 抛出的无效参数异常

相关文章

最新文章