gpt4 book ai didi

iphone - 为 UIImageView 实现 UITouchDown

转载 作者:行者123 更新时间:2023-12-03 18:34:52 24 4
gpt4 key购买 nike

我知道如何为UIImageView实现touchesBegan

是否可以为UIImageView实现UITouchDown?(我知道我可以使用touchesBegan而不是UITouchDown > 但我想实现 UITouchDown)

最佳答案

使用 UIButton。

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:someUIImage forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:aButton];

UIButton 的替代方案(较长的方法)

UIControl 已经实现了用户交互并支持细粒度的用户交互。您可以结合 UIImageViewUIControl 的功能来实现此目的,因为它们都是 UIView 的子类。

要获得此行为,请将 UIImageView 对象作为 subview 添加到 UIControl 中,以便完全覆盖图像。然后使用addTarget:action:forControlEvents:向该控件添加事件处理程序。这是一个例子:

// assuming the image view object exists
UIImageView *anImageView = ..;

// create a mask that the covers the image exactly
UIControl *mask = [[UIControl alloc] initWithFrame:anImageView.frame];

// add the image as a subview of this mask
CGSize imageSize = anImageView.frame.size;
anImageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
[mask addSubview:anImageView];

// add a target-action for the desired control events to this mask
[mask addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];

// add the mask as a subview instead of the image
[self.view addSubview:mask];

关于iphone - 为 UIImageView 实现 UITouchDown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4930449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com