简体   繁体   English

基于UITouch旋转UIView

[英]Rotate UIView based on UITouch

I'm working on a drawing app. 我正在开发绘图应用程序。

I want the user to be able to "drop" a shape on the screen and then move, resize or rotate it as desired. 我希望用户能够在屏幕上“放置”一个形状,然后根据需要移动,调整大小或旋转它。

The problem is with the rotation. 问题出在旋转上。 I have the moving and resizing working fine. 我的搬动和调整大小工作正常。

I did this before with a rather complex and memory/processor-intensive process, which I am now trying to improve. 之前,我是通过一个相当复杂且需要大量内存/处理器的过程来完成此任务的,现在我正在尝试改进这一过程。

I've searched and searched but haven't found an answer similar to what I'm trying to do. 我已经搜索了,但是没有找到与我尝试的类似的答案。

Basically, let's say the user drops a square on the "surface". 基本上,假设用户在“表面”上放置了一个正方形。 Then, they tap it and get some handles. 然后,他们点击它并得到一些手柄。 They can touch anywhere and pan to move the square around (working already), touch and drag on a resize handle to resize the square (working already), or grab the rotation handle to have the square rotate around its center. 他们可以触摸任意位置并平移以移动正方形(已工作),触摸并拖动其大小调整手柄以调整正方形(已工作),或抓住旋转手柄以使正方形围绕其中心旋转。

I've looked into drawing the square using UIBezierPath or just having it be a subclass of UIView that I fill. 我已经研究过使用UIBezierPath绘制正方形,或者只是将其作为我填充的UIView的子类。

In either case, I'm trying to rotate the UIView itself, not some contents inside. 无论哪种情况,我都试图旋转UIView本身,而不旋转其中的某些内容。 Every time I try to rotate the view, either nothing happens, the view vacates the screen or it rotates just a little bit and stops. 每次尝试旋转视图时,要么什么都没有发生,要么视图离开屏幕,要么旋转一点并停止。

Here's some of the code I've tried (this doesn't work, and I've tried a lot of different approaches to this): 这是我尝试过的一些代码(这不起作用,并且我尝试了很多不同的方法):

- (void) rotateByAngle:(CGFloat)angle
{

CGPoint cntr = [self center];

CGAffineTransform move  = CGAffineTransformMakeTranslation(-1 * cntr.x, -1 * cntr.y);
[[self path] applyTransform:move];

CGAffineTransform rotate = CGAffineTransformMakeRotation(angle * M_PI / 180.0);
[[self path] applyTransform:rotate];

[self setNeedsDisplay];
CGAffineTransform moveback = CGAffineTransformMakeTranslation(cntr.x, cntr.y);
[[self path] applyTransform:moveback];

}

In case it isn't obvious, the thinking here it to move the view to the origin (0,0), rotate around that point and then move it back. 如果不明显,这里考虑将视图移至原点(0,0),围绕该点旋转然后再移回原点。

In case you're wondering, "angle" is calculated correctly. 如果您想知道,“角度”是正确计算的。 I've also wrapped the code above in a [UIView beginAnimations:nil context:NULL] / [UIView commitAnimations] block. 我还将上面的代码包装在[UIView beginAnimations:nil context:NULL] / [UIView commitAnimations]块中。

Is it possible to rotate a UIView a "custom" amount? 是否可以将UIView旋转为“自定义”数量? I've seen/done it before where I animate a control to spin, but in those examples, the control always ended up "square" (ie, it rotated 1 or more full circles and came back to its starting orientation). 在为控件旋转动画之前,我已经看过/完成过该控件,但是在那些示例中,该控件始终以“正方形”结束(即,它旋转了1个或更多个完整的圆并返回其初始方向)。

Is it possible to perform this rotation "real-time" in response to UITouches? 是否可以响应UITouches进行“实时”旋转? Do I need to draw the square as an item in the layer of the UIView and rotate the layer instead? 我是否需要将正方形绘制为UIView图层中的一项,然后旋转该图层?

Just so you know, what I had working before was a shape drawn by a set of lines or UIBezierPaths . 众所周知,我之前所做的是由一组线或UIBezierPaths绘制的形状。 I would apply a CGAffineTransform to the data and then call the drawRect: method, which would re-draw the object inside of a custom UIView. 我将CGAffineTransform应用于数据,然后调用drawRect:方法,该方法将在自定义UIView内重新绘制对象。 This UIView would host quite a number of these items, all of which would need to be re-drawn anytime one of them needed it. 这个UIView可以容纳很多这样的项目,所有这些项目都需要在其中任何一个需要的时候重新绘制。

So, I'm trying to make the app more performant by creating a bunch of UIView subclasses, which will only get a command to re-draw when the user does something with them. 因此,我正在尝试通过创建一堆UIView子类来提高应用程序的性能,这些子类将仅在用户对其执行某些操作时获得重新绘制的命令。 Apple's Keynote for the iPad seems to accomplish this using UIGestureRecognizers, since you have to use two fingers to rotate an added shape. Apple的iPad Keynote似乎可以使用UIGestureRecognizers完成此操作,因为您必须使用两个手指来旋转添加的形状。 Is this the way to go? 这是要走的路吗?

Thoughts? 思考?

Thanks! 谢谢!

-(void)rotate{
    CGAffineTransform transform;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve:UIViewAnimationOptionBeginFromCurrentState];
    myView.alpha = 1;
    transform = CGAffineTransformRotate(myView.transform,0.5*M_PI);
    [myView setUserInteractionEnabled:YES];
    myView.transform = transform;
    [UIView commitAnimations];
}

This might help. 这可能会有所帮助。

for just simple rotation you might leave out the Animation: 只需简单的旋转,您就可以省略动画:

-(void)rotate:(CGFloat)angle
{
    CGAffineTransform  transform = CGAffineTransformRotate( myView.transform, angle * M_PI / 180.0 );
    myView.transform = transform;
}

I use a simple NSTimer to control a animation. 我使用一个简单的NSTimer来控制动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM