我是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是可以的。
所以我弄错了?请给我解释一下!感谢大家的支持!
发布于 2014-05-13 09:24:19
touchesBegan接收整个视图控制器的触摸,而不仅仅是一个uiview。如果将userInteractionEnabled设置为false,视图将不会收到任何特定于视图的事件,如UITouchUpInside。
如果要签入touchesBegan方法,如果用户单击了“禁用”对象,则必须获得用户触摸ViewController的坐标,例如,如下所示
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];,而不是检查touchPoint是否位于要检查的视图对象的矩形内。
if (CGRectContainsPoint(cuiv, touchPoint){
if (cuiv.userInteractionEnabled) {
// your element is enabled, do something
} else {
// your element is disabled, do something else
}
}https://stackoverflow.com/questions/23626305
复制相似问题