0

我已經創建了一個UIView,適用於UIPanGestureRecognizer以便用一個手指旋轉,並且還可以通過一個手指增加和減小其框架。旋轉UIView的框架在iPhone中通過UIPanGesture增加減少量

  • 旋轉視圖放在UIView的左下角。
  • 調整視圖放在UIView的右下角。

讓我寫下一些代碼行。

//Rotate Apply with Pan Gesture 
    panRotateGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateViewPanGesture:)]; 
    [rotateView addGestureRecognizer:panRotateGesture]; 

    //Resize View Apply with Pan Gesture 
    panResizeGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(resizeTranslate:)]; 
    [resizeView addGestureRecognizer:panResizeGesture]; 

    [panRotateGesture requireGestureRecognizerToFail:panResizeGesture]; 

這裏是用UIPanGesture用單指調整視圖大小的方法。

-(void)resizeTranslate:(UIPanGestureRecognizer *)recognizer 
{ 
    if ([recognizer state]== UIGestureRecognizerStateBegan) 
    { 
     prevPoint = [recognizer locationInView:self.superview]; 
     [self setNeedsDisplay]; 
    } 
    else if ([recognizer state] == UIGestureRecognizerStateChanged) 
    { 
     if (self.frame.size.width < kFixW) 
     { 
      self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, kFixW, self.frame.size.height); 
     } 

     if(self.frame.size.height < kFixH) 
     { 
      self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, kFixH); 
     } 

     CGPoint point = [recognizer locationInView:self.superview]; 
     float wChange = 0.0, hChange = 0.0; 

     wChange = (point.x - prevPoint.x); //Slow down increment 
     hChange = (point.y - prevPoint.y); //Slow down increment 


     self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width + (wChange), self.frame.size.height + (hChange)); 
     NSLog(@"self.frame : %@",NSStringFromCGRect(self.frame)); 

     prevPoint = [recognizer locationInView:self.superview]; 

     [self setNeedsDisplay]; 
    } 
    else if ([recognizer state] == UIGestureRecognizerStateEnded) 
    { 

     prevPoint = [recognizer locationInView:self.superview]; 
     [self setNeedsDisplay]; 
    } 
} 

在這裏,如果有UIPanGesture用一個手指

-(void)rotateViewPanGesture:(UIPanGestureRecognizer *)recognizer 
{ 
    if ([recognizer state] == UIGestureRecognizerStateBegan) 
    { 
     deltaAngle = atan2([recognizer locationInView:self].y-self.center.y, [recognizer locationInView:self].x-self.center.x); 
     startTransform = self.transform; 
    } 
    else if ([recognizer state] == UIGestureRecognizerStateChanged) 
    { 
     float ang = atan2([recognizer locationInView:self.superview].y - self.center.y, [recognizer locationInView:self.superview].x - self.center.x); 
     float angleDiff = deltaAngle - ang; 
     self.transform = CGAffineTransformMakeRotation(-angleDiff); 
     [self setNeedsDisplay]; 
    } 
    else if ([recognizer state] == UIGestureRecognizerStateEnded) 
    { 
     float ang = atan2([recognizer locationInView:self.superview].y - self.center.y, [recognizer locationInView:self.superview].x - self.center.x); 
     deltaAngle = atan2([recognizer locationInView:self].y-self.center.y, [recognizer locationInView:self].x-self.center.x); 
     startTransform = self.transform; 
     [self setNeedsDisplay]; 
    } 
} 

旋轉的UIView現在的問題是, 都調整大小和旋轉手勢正常工作。

當我使用panResizeGesture調整視圖的大小時,它可以正常工作,但是如果我應用一次panRotateGesture並且想要應用panResizeGesture,它會熄滅屏幕並且無法正常工作。

任何想法來解決這個問題,請。

非常感謝。

回答

0

我知道這有點晚了,所以也許你已經知道了,但你的問題是在locationInView:你的代碼部分。當您真的想要計算superview的locationInView時,您正試圖計算您想要旋轉的實際視圖的locationInView。

-(void)rotateViewPanGesture:(UIPanGestureRecognizer *)recognizer{ 
if ([recognizer state] == UIGestureRecognizerStateBegan) 
{ 
    deltaAngle = atan2([recognizer locationInView:self.superview].y-self.center.y, [recognizer locationInView:self.superview].x-self.center.x); //here change it to self.superview 
    startTransform = self.transform; 
} 
else if ([recognizer state] == UIGestureRecognizerStateChanged) 
{ 
    float ang = atan2([recognizer locationInView:self.superview].y - self.center.y, [recognizer locationInView:self.superview].x - self.center.x); 
    float angleDiff = deltaAngle - ang; 
    self.transform = CGAffineTransformMakeRotation(-angleDiff); 
    [self setNeedsDisplay]; 
} 
else if ([recognizer state] == UIGestureRecognizerStateEnded) 
{ 
    float ang = atan2([recognizer locationInView:self.superview].y - self.center.y, [recognizer locationInView:self.superview].x - self.center.x); 
    deltaAngle = atan2([recognizer locationInView:self.superview].y-self.center.y, [recognizer locationInView:self.superview].x-self.center.x); //here change it to locationInView:self.superview 
    startTransform = self.transform; 
    [self setNeedsDisplay]; 
}} 
2

documentation明確表示:

**如果變換屬性不是恆等變換,這個屬性的值是不確定的,因此應被忽略。

如果transform屬性包含非標識轉換,frame屬性的值是未定義的,不應該修改。在這種情況下,您可以使用中心物業重新定位視圖,並使用bounds屬性,而不是調整大小。**

因此,只要改變self.frame到self.bounds在rotateViewPanGesture方法

例如

 self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, kFixH); 

到:

 self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, kFixH); 

,你的代碼將完全正常工作。

更新

我重視其用於您的代碼示例項目。您可以使用此代碼 Answer

+0

您確定它會起作用嗎?請深入閱讀問題.. –

+0

是的。我也有同樣的proplem,我通過以上方式解決它 – thavasidurai

+0

你檢查我的回答 – thavasidurai