子类放大UIScrollView没有委托方法

子类放大UIScrollView没有委托方法

本文介绍了子类放大UIScrollView没有委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个 UIScrollView 子类来呈现一些自定义格式化的内容。我只是设置滚动视图的模型对象属性,它处理所有必需的布局和渲染以显示内容。

I want to implement a UIScrollView subclass to present some custom formatted content. I just set a model object property of the scroll view and it handles all the required layout and rendering to display the content.

这个工作正常,但现在我想包括缩放。根据文档,要支持缩放,你必须设置一个委托并实现 viewForZoomingInScrollView:方法。我想我可以设置委托到滚动视图本身,并在子类中实现该方法。但是,这样做会失去一个外部代理(如封装的UIViewController)的能力,可以通知滚动事件。

This works fine, but now I'd like to include zooming. According to the documentation, to support zooming you have to set a delegate and implement the viewForZoomingInScrollView: method. I guess I could set the delegate to the scroll view itself and implement that method in the subclass. But doing that I would lose the ability to have an external delegate (like an encapsulating UIViewController) that can be notified about scroll events.

假设文档是正确的,绝对没有(文档化)的方式实现缩放没有委托,我怎么能仍然保留有可能性有一个规则,无关的委托?

Assuming the documentation is right and there is absolutely no (documented) way to implement zooming without a delegate, how could I still retain the possibility of having a regular, unrelated delegate?

推荐答案

我滥用这个事实,我是一个子类(有意的:P)。所以你可以攻击它。真的很糟糕,对于提出这个解决方案,我感觉不太好。

I'd abuse the fact that I'm being a subclass (on purpose :P). So you can hack it. Really bad, and I should feel bad for proposing this solution.

@interface MyHackishScrollView: UIScrollView {
    id <UIScrollViewDelegate> ownDelegate;
}

@end

@implementation MyHackishScrollView

- (void)setDelegate:(id <UIScrollViewDelegate>)newDel
{
    ownDelegate = newDel;
    [super setDelegate:self];
}

- (UIView *)viewForScrollingInScrollView:(UIScrollView *)sv
{
    return whateverYouWant;
}

// and then implement all the delegate methods
// something like this:
- (void)scrollViewDidScroll:(UIScrollView *)sv
{
    [ownDelegate scrollViewDidScroll:self];
}

// etc.

@end

这篇关于子类放大UIScrollView没有委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:16