我已經創建了一個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,它會熄滅屏幕並且無法正常工作。
任何想法來解決這個問題,請。
非常感謝。
您確定它會起作用嗎?請深入閱讀問題.. –
是的。我也有同樣的proplem,我通過以上方式解決它 – thavasidurai
你檢查我的回答 – thavasidurai