使用UIGestures删除选定的uiimage?

时间:2012-11-02 00:25:15

标签: iphone uiview uiimage gestures

我正试图通过长按手势删除图像... 问题是我可以在同一视图上有多个图像。并且可能希望通过按住图像来删除每个图像。

所以我想说:

 @property (nonatomic, retain) IBOutlet UIImageView *Image1;
 @property (nonatomic, retain) IBOutlet UIImageView *Image2;
 @property (nonatomic, retain) IBOutlet UIImageView *Image3;

都在同一个视图中。 现在我按住Image1。 如何让Longpress手势知道我选择了Image1 ??

- (void)handleDelete:(UILongPressGestureRecognizer *)recognizer {
   //Something in here to detect image is selected and delete :)
 }

感谢!!!

2 个答案:

答案 0 :(得分:2)

在.h文件中提供UIGestureRecognizerDelegate,然后尝试使用此代码......

    UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
    longpressGesture.minimumPressDuration = 5;
    [longpressGesture setDelegate:self];
    [self.yourImages addGestureRecognizer:longpressGesture];
    [longpressGesture release];

    - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
        NSLog(@"longPressHandler");
        UIImageView *tempImage=(UIImageView*)[gestureRecognizer view];
        [tempImage removeFromSuperview];
    }

答案 1 :(得分:1)

UILongPressGestureRecognizer添加到单个图像中,然后在检测到手势时将其删除,如下所示:

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)];
longPressRecognizer.minimumPressDuration = 3;
longPressRecognizer.numberOfTouchesRequired = 1;
[self.Image1 addGestureRecognizer:longPressRecognizer];
[self.Image2 addGestureRecognizer:longPressRecognizer];
[self.Image3 addGestureRecognizer:longPressRecognizer];

- (IBAction)longPressDetected:(UILongPressGestureRecognizer *)sender {
    NSLog(@"Long Press");
    // Delete/Remove sender object
}