我创建了一个UIView,应用UIPanGestureRecognizer用一根手指旋转,并用一个手指增加和减少它的框架。
让我写下一些代码行。
//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它会超出屏幕并且无法正常工作。
请解决这个问题。
非常感谢。
答案 0 :(得分:2)
documentation清楚地说:
**如果transform属性不是identity变换,则此属性的值是未定义的,因此应该被忽略。
如果transform属性包含非标识变换,则frame属性的值是未定义的,不应修改。在这种情况下,您可以使用center属性重新定位视图,并使用bounds属性调整大小。**
所以,只需将rotate.frame更改为rotateViewPanGesture方法中的self.bounds
即可例如
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
答案 1 :(得分: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];
}}