我创建了一个 UIView 并应用 UIPanGestureRecognizer 用一根手指旋转,并用一根手指增加和减少它的框架。
- 旋转视图放在 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 旋转 UIView
-(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];
}
}
现在的问题是,调整大小和旋转手势都可以正常工作。
当我使用 panResizeGesture 调整视图大小时,它可以正常工作,但是如果我应用一次 panRotateGesture 并且想要应用 panResizeGesture 它会超出屏幕并且无法正常工作。
请有任何想法来解决这个问题。
非常感谢。