3

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

所以可以说我有:

 @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 :)
 }

谢谢!!!

4

2 回答 2

2

提供UIGestureRecognizerDelegatein .h 文件,然后尝试此代码...

    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];
    }
于 2012-11-02T05:04:43.060 回答
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
}
于 2012-11-02T00:54:57.547 回答