嘗試是確保識別器正在「同時」的第一件事情。在你的代表中,你需要定義以下內容:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return (gestureRecognizer.view == otherGestureRecognizer.view);
}
根據我的經驗,這足以提供足夠順暢的體驗。但是,如果不是,那麼我只會從其中一個識別器更新變換,將另一個狀態存儲在屬性中。假設您已聲明一個名爲cachedScale的CGFloat
屬性,該屬性在初始化程序中設置爲1.0。然後在你捏和平移處理程序,你會做到以下幾點:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
self.cachedScale *= recognizer.scale;
}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGAffineTransform transform = CGAffineTransformMakeScale(self.cachedScale, self.cachedScale);
self.cachedScale = 1.0;
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
CGAffineTransformTranslate(transform, translation.x, translation.y);
// do something with your transform
}
如果你只是想隨意拖動一個視圖,你可能有更多的運氣改變視圖的中心,而不是應用轉換到它的轉變。