Iphone 如何获取UIImageView I';我在窃听?
我有几个UIImageView,每个都有一个标签;我有一个图像数组,我想做的是:当用户点击其中一个UIImageView时,应用程序会从数组中返回特定的图像 我是这样实现的:Iphone 如何获取UIImageView I';我在窃听?,iphone,tags,uigesturerecognizer,Iphone,Tags,Uigesturerecognizer,我有几个UIImageView,每个都有一个标签;我有一个图像数组,我想做的是:当用户点击其中一个UIImageView时,应用程序会从数组中返回特定的图像 我是这样实现的: - (void)viewDidLoad { [super viewDidLoad]; scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; [self.view addSubview:scroll];
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<8; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)];
imageView.backgroundColor = [UIColor blueColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(@"%d", imageView.tag);
[scroll addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(findOutTheTag:)];
[imageView addGestureRecognizer:tap];
}
scroll.contentSize = CGSizeMake(320, 115*i);
}
- (void)findOutTheTag:(id)sender
{
// HOW TO FIND THE tag OF THE imageView I'M TAPPING?
}
显示图像
我确实标记了它们,问题是如何找到我正在点击的imageView的
标记?感谢您阅读^ ^,冒着没有看到应用程序全貌而提出建议的风险,为什么不使用自定义UIButton而不是UIImageViews?使用UIButton,您可以设置一个操作并传递发送者id,从中可以轻松访问标记并从阵列中提取数据
或者,如果您确实想使用上述代码,并且您知道正在调用-(void)findOutTheTag:(id)sender方法,那么您需要做的就是:
- (void)findOutTheTag:(id)sender {
switch (((UIGestureRecognizer *)sender).view.tag)
{
case kTag1:
//...
case kTag2:
//...
}
}
与其使用UIImageView,不如使用UIButton。这样,您就可以简单地为UITouchDown事件添加一个侦听器。您可以标记每个按钮,以便在触地方法中找到按下的按钮
UIButton *button = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)];
button.backgroundColor = [UIColor blueColor];
button.tag = i;
[button addTarget:self action:@selector(touchDown:) controlEvent:UIControlEventTouchDown];
在触地:方法中,您只需将发送者强制转换为UIButton,即可访问标记
- (void)touchDown:(id)sender
{
UIButton* button = (UIButton*)sender;
switch(button.tag)
{
case TAG1:
break;
//etc
}
}
要找出必须触摸的图像,请使用touchStart方法:
注意:首先,您需要确认图像视图应为userIntrectionEnabled=YES代码>
现在使用此方法:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view].tag == 800) {
//if image tag mated then perform this action
}
}
你可以在touchStart中使用switch语句,这里的发送者是手势识别器,所以你实际上必须执行切换(((UIgestureRecognitor*)发送者.view.tag)
+1-(void)childTapped:(UITapstureRecognitzer*)Tap手势{int tag Tap手势.view.tag;}//为我工作过,DHamrick,我尝试了UIButton而不是UIImageView,它完美地输出了标记,确实帮助我跳出了自己的思维,但使用UIButton可能会实现许多手势,以实现我需要使用的其他手势,如长按和滑动,移动/删除视图项(还涉及标记和数组),在我的构造中可能会变得复杂,由于UIGestureRecognitor已经有了它们,我决定使用它们,无论如何,非常感谢您的帮助,我会将应用程序发送给您^_^
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view].tag == 800) {
//if image tag mated then perform this action
}
}