我如何选择点击手势上的图像。我想添加与我点击的图像相同的图像。如果我有3张图片,并且我点击了第二张图片,那么它将动态添加与第二张图片相同的第四张图片。
UIImage *image = [[info objectForKey:@"UIImagePickerControllerOriginalImage"] retain];
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width/4, image.size.height/4)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
-(void)tapped:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
NSLog(@"Tapped");
[self.view bringSubviewToFront:[(UITapGestureRecognizer*)sender view]];
UIImageView *image = (UIImageView *)[(UITapGestureRecognizer*)sender view];
[self.view addSubview:image];
}
我正在使用
[(UITapGestureRecognizer*)sender view]]
获得该视图。但是我不能。在点击时,我想添加我点击过的图像。 最佳答案
问题出在您的窃听方法上。当您编写(UIImageView *)[(UITapGestureRecognizer*)sender view];
时,您将获得相同的添加的imageView
,其框架将与添加时的框架相同。然后,使用addSubview
添加它,如果这样做,它将被添加到之前添加的位置。
只需将tapped
更改为:
-(void)tapped:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
UIImageView *imgView = (UIImageView *)[(UITapGestureRecognizer*)sender view];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:yourFrame]; //Set the next frame for the image view
imageView.image = imgView.image;
[self.view addSubview:imageView];
[imageView release];
imageView = nil;
}
还将手势添加到UIImageView中,如下所示:
[imageview addGestureRecognizer:tapRecognizer];
不用于UIView。同时设置
imageview.userInteractionEnabled = YES;
关于iphone - 在iPhone中选择点击手势上的图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13451774/