改进触摸事件 UIImageView

发布于 2024-11-27 12:57:19 字数 2782 浏览 2 评论 0原文

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

下面的代码显示我有 2 个小图像(一个使用 UIImageView,另一个使用 UIView),其中我已附加到 UIScrollView,该图像附加到来自的 UIView与附加到 UIWindow 的 UIViewController 一起使用。非常基本。

我还在 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,但在上面的代码中没有使用它..没有发布..可能有一些子类化错误(子类化的新内容(将是诚实))但一切都非常感激!而且,我希望其他人也能得到帮助!

I have 30-40 tabs open, I've found my answer.. but I'm wishing to improve.
The code does work, I am just not sure if it is efficient

The following code shows that I have 2 small images (One using UIImageView, another using UIView) in which I have attached to a UIScrollView, which is attached to a UIView which came with the UIViewController, which is attached to the UIWindow. Pretty basic.

I also have a button attached to the UIViewController to make sure zooming and scrolling worked. (By testing the static location of the button and size)

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

Implementation:

- (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];
}

You'll see near the bottom I have a TestSubclass class made.
The code for that is as follows:

@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

The part in which I am happy with is that I have a fully functioning Subclass of UIImageView which works as if it was a UIButton.
BUT! without the 7 backgroundImageViews (and the overhead from each of those), without the 6 labels (and the overhead from each of those), without the 6 UIImageViews ("..."), and lastly the amount of un-needed things that UIButton offers. I only wanted touch capabilities.. efficiently however. (TouchUpInside, Dragging, etc)

But, now during my research (after the extensive testing to accomplish this much), I've come across in documentation and some examples addGestureRecognizer in UIView. It seems sorta-but not entirely efficient for my needs.

I'm looking for advice, I know there are a couple problems in my code (overrided init, but didn't use it in the above code.. didn't release.. probably some subclassing mistakes (new to subClassing (to be honest)). But anything is much appreciated!! And, I hope others are helped as well!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-12-04 12:57:19

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

If you want to have full control capabilities, you must subclass UIControl and use your image with in. The image can be an UIImageView subview or can be drawn in the drawRect method of your subclass or set in the CALayer view content.
Note that subclassing UIControl is not the easiest thing to do, but it gives you all "control" feature it seems you are requesting from it and I think is the most efficient or appropriate approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文