2014-02-28 102 views
2

我想在將其添加到屏幕截圖後立即啓動UIPanGestureRecognizer。因爲截圖是通過代碼創建的,所以當某個項目被突出顯示時,用戶將不會再次按下屏幕。所以...我如何以編程方式啓動識別器?以編程方式啓動UIGestureRecognizer

UIView *snapshot = [cell snapshotViewAfterScreenUpdates:NO]; 

    //use the cell to map the snapshot frame to the window because this does a perfect job of accounting for table offset, etc. Other methods put the view a little to the side or way off 
    CGRect newFrame = snapshot.frame; 
    newFrame.origin = [cell convertPoint:newFrame.origin toView:self.view.window]; 
    [snapshot setFrame:newFrame]; 

    [HelperMethods shadowForView:cell color:[UIColor blackColor] offset:CGSizeMake(1, 1) opacity:.7 radius:snapshot.frame.size.width/4]; 

    //[self.view addSubview:snapshot]; 
    newFrame.origin.y -=10; 

    //move the frame a little to let user know it can be moved 
    [UIView animateWithDuration:.2 animations:^{ 
     [snapshot setFrame:newFrame]; 
    }]; 

    //add a long press that kills the tap if recognized 
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(userCellDragged:)]; 
    [pan setMinimumNumberOfTouches:1]; 
    [pan setMaximumNumberOfTouches:1]; 

    [cell addGestureRecognizer:pan]; 
+1

通過調用其目標方法? – Larme

+0

所以,你說什麼是你想觸發「userCellDragged」方法,你正試圖弄清楚如果它實際上是由手勢識別器觸發它將收到的參數傳遞? – ezekielDFM

+0

正確。通過代碼 –

回答

2

您可以隨時調用自身的方法。

例如,您已經添加了選擇

- (void) userCellDragged:(UIPanGestureRecognizer)sender; 

爲了您的移動手勢識別。

您可以從任何地方在視圖中通過簡單地添加

[self userCellDragged:nil]; 

稱之爲記住一個參數添加到這個方法是這樣的:

if (sender == nil) { 
    // Triggered programmatically 
} 
else { 
    // proceed as normal 
} 
0

可以更好地利用UIGestureRecognizer將工作沒有「開始」挖掘