【问题标题】:Improve Touch Events UIImageView改进触摸事件 UIImageView
【发布时间】:2011-10-19 11:10:01
【问题描述】:

我打开了 30-40 个标签,我找到了答案。但我希望改进。 该代码确实有效,我只是不确定它是否有效

以下代码显示我有 2 个小图像(一个使用 UIImageView,另一个使用 UIView),其中我已附加到 UIScrollView,该 UIScrollView 附加到 UIViewController 附带的 UIView,该 UIViewController 附加到 UIWindow。很基本。

我还在 UIViewController 上附加了一个按钮,以确保缩放和滚动正常工作。 (通过测试按钮的静态位置和大小)

@interface Wire_TestViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollview;
}
@end

实施:

- (void)test:(UIButton *)sender {  }
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return scrollview; }
- (void)viewDidLoad {
    [super viewDidLoad];

    scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    scrollview.delegate = self;
    scrollview.contentSize = CGSizeMake( 320*2, 480*2 );
    scrollview.minimumZoomScale = .1;
    scrollview.maximumZoomScale = 10;
    [self.view addSubview:scrollview];

    UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(320, 320, 20, 20)];
    view.image = [UIImage imageNamed:@"Node.png"];
    view.clearsContextBeforeDrawing = NO;
    [scrollview addSubview:view];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(10, 10, 100, 30);
    button.tag = 51;
    [button setTitle:@"new button" forState:(UIControlState)UIControlStateNormal];
    [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 30, 30)];
    view2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"WireActive.png"]];
    [scrollview addSubview:view2];

    TestSubclass *test = [[TestSubclass alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    test.userInteractionEnabled = TRUE;
    test.image = [UIImage imageNamed:@"Node.png"];
    [scrollview addSubview:test];
}

您会在底部附近看到我制作了一个 TestSubclass 类。 代码如下:

@interface TestSubclass : UIImageView {  }
@end

@implementation TestSubclass
- (id)init {
    [super init];
    self.userInteractionEnabled = TRUE;
    return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    printf("Being touched...\n");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    printf("Touches Ended...\n");
}
@end

令我满意的部分是我有一个功能齐全的 UIImageView 子类,它就像一个 UIButton 一样工作。 但!没有 7 个 backgroundImageViews(以及每个标签的开销),没有 6 个标签(以及每个标签的开销),没有 6 个 UIImageViews(“...”),最后是不需要的东西的数量UIButton 提供。我只想要触摸功能..但是有效。 (TouchUpInside、拖动等)

但是,现在在我的研究过程中(经过大量测试以完成这项工作),我在文档和一些示例中遇到了 UIView 中的 addGestureRecognizer。这似乎有点——但对我的需求并不完全有效。

我正在寻求建议,我知道我的代码中有几个问题(重写了 init,但没有在上面的代码中使用它.. 没有发布.. 可能是一些子类化错误(子类化的新手(说实话))。但是任何事情都非常感谢!!而且,我希望其他人也能得到帮助!

【问题讨论】:

    标签: iphone objective-c c uiimageview touch


    【解决方案1】:

    如果你想拥有完整的控制能力,你必须继承 UIControl 并使用你的图像。图像可以是 UIImageView 子视图,也可以在你的子类的 drawRect 方法中绘制或在 CALayer 视图内容中设置。 请注意,子类化 UIControl 并不是最简单的事情,但它为您提供了您似乎要求的所有“控制”功能,我认为这是最有效或最合适的方法。

    【讨论】:

    • 我知道 UIControl 本身很高效,但我偏离它的原因是它也有 UIView 作为它的超类,而 UIView 本身就是很多包袱,而应用程序是将需要尽可能多的空间,因为它可以免费获得。除非您可能知道不使用 UIImageView 来绘制图像的方法?但我相信只有 UIImageView 完成了显示 bmp、jpg 和 png 图像的编码。遗憾的是。因此,我改为将其子类化。
    • UIImageView 也是 UIView 的子类。您可以通过将任何 UIView 的 layer.contents 属性设置为 CGImageRef 来绘制没有 UIImageView 的图像。阅读内容属性的 CALayer 文档,不要忘记每个视图都由 CALayer 类支持。
    • 你刚刚扔给我一把!哈哈!谢谢!虽然,作为一个简单的问题:UIView 中的属性,例如 .center 是否与图层交互,我看到 CALayer 处理绘图,但它也有大量的属性!
    猜你喜欢
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多