当我触摸并按住图像2秒钟时,我正试图拨打警报框.这是我到目前为止所得到的:
@H_301_2@- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *tapAndHoldGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAndHoldGesture:)];
tapAndHoldGesture.minimumPressDuration = 0.1;
tapAndHoldGesture.allowableMovement = 600;
[self.view addGestureRecognizer:tapAndHoldGesture];
}
- (void) handleTapAndHoldGesture:(UILongPressGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Gesture:" message:@"hold it" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
不确定这是否会产生任何影响,但是Image View是以后以编程方式创建的,而不是在加载时.提前感谢您的任何帮助表示赞赏..
另外,我看了以下链接:
Long press gesture on UICollectionViewCell
解决方法
@H_301_2@-(void)viewDidLoad
{
[super viewDidLoad];
[self setupGesture];
}
-(void) setupGesture
{
UILongPressGestureRecognizer *lpHandler = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleHoldGesture:)];
lpHandler.minimumPressDuration = 1; //seconds
lpHandler.delegate = self;
//myUIImageViewInstance - replace for your instance/variable name
[**myUIImageViewInstance** addGestureRecognizer:lpHandler];
}
- (void) handleHoldGesture:(UILongPressGestureRecognizer *)gesture
{
if(UIGestureRecognizerStateBegan == gesture.state)
{
// Called on start of gesture,do work here
}
if(UIGestureRecognizerStateChanged == gesture.state)
{
// Do repeated work here (repeats continuously) while finger is down
}
if(UIGestureRecognizerStateEnded == gesture.state)
{
// Do end work here when finger is lifted
}
}