gpt4 book ai didi

ios - 关于 iOS 中的 setUserInteractionEnabled UIImageView

转载 作者:行者123 更新时间:2023-11-28 20:01:57 24 4
gpt4 key购买 nike

我是 iOS 新手,我对 setUserInteractionEnabled 有疑问: 我有一个从 UIImageView 扩展的对象,例如:

@interface CustomImageView : UIImageView

@property (retain, nonatomic) NSString *sNameImage;
@property (retain, nonatomic) NSString *sID;
@property (assign, nonatomic) float nPointX;
@property (assign, nonatomic) float nPointY;

-(void)setPoint:(NSString *)sPoint;

然后当我将它添加到 UIView 时:

for(int i = 0; i < self.arrCustomImageView.count; i++)
{
CustomImageView *cuiv = [self.arrCustomImageView objectAtIndex:i];
NSData *data = [self readDataFromFile:cuiv.sNameImage];
if(data != nil && data.length > 0)
{
UIImage *image = [[UIImage alloc] initWithData:data];
CGRect r = CGRectMake(cuiv.nPointX, cuiv.nPointY, image.size.width, image.size.height);
[cuiv setUserInteractionEnabled:FALSE]; /*or NO i tried all*/
[cuiv setImage:image];
cuiv.frame = r;
[self addSubview:cuiv];
[image release];
image = nil;
}
}

然后在 touchesBegan 中,我仍然在触摸它时捕获事件(触摸它时我仍然收到日志):|

然后源码touchBegan方法:

UITouch * touch = [touches anyObject];
for (UIView *aView in [self subviews])
{
if (CGRectContainsPoint([aView frame], [touch locationInView:self]))
{
self.imvChoice = (CustomImageView *) aView;
originalPoint = aView.frame.origin;
offsetPoint = [touch locationInView:aView];
nIndexChoice = [self.arrCustomImageView indexOfObject:aView];
NSLog(@"log: %@ >> %@: id: %d", NSStringFromClass([self class]),NSStringFromSelector(_cmd), nIndexChoice);
[self bringSubviewToFront:aView];
}
}

我不认为这是问题,因为我已经尝试添加相同的 UIImageView,如下所示:

 for(int i = 0; i < [arrObject count]; i++)
{
CustomObject *nsObj = [arrObject objectAtIndex:i];

UIImageView *image1 = [self createUIImageView:[nsObj sName1] pointX:nsObj.pPoint1.x pointY:nsObj.pPoint1.y];
image1.tag = i;
[image1 setUserInteractionEnabled:FALSE];

UIImageView *image2 = [self createUIImageView:[nsObj sName2] pointX:nsObj.pPoint2.x pointY:nsObj.pPoint2.y];
image2.tag = i;
[image2 setUserInteractionEnabled:TRUE];

[self addSubview:image1];
[self addSubview:image2];
[image1 release];
[image2 release];
}

它运行正常:|单击 image1 时我无法捕获事件,而 image2 可以。

所以我错了?请帮我解释一下!感谢大家的支持!

最佳答案

touchesBegan 接收整个 View Controller 的触摸,而不仅仅是单个 uiview。如果您将 userInteractionEnabled 设置为 false,则该 View 将不会收到任何特定于 View 的事件,例如 UITouchUpInside

如果你想检查你的 touchesBegan 方法,如果用户点击了一个“禁用”的对象,你必须获得用户触摸你的 ViewController 的坐标,例如像这样

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];

然后检查您的触摸点是否在您要检查的 View 对象的矩形内

if (CGRectContainsPoint(cuiv, touchPoint){
if (cuiv.userInteractionEnabled) {
// your element is enabled, do something
} else {
// your element is disabled, do something else
}
}

关于ios - 关于 iOS 中的 setUserInteractionEnabled UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626305/

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