質問

I have a UIView sublass that draws some stuff in drawRect in this way:

CGContextRef contextRef = UIGraphicsGetCurrentContext();

CGContextSetRGBFillColor(contextRef, 0.2, 1, 0.2, 0.8);

CGContextFillEllipseInRect(contextRef, CGRectMake(24, 24, 4, 4));
CGContextFillEllipseInRect(contextRef, CGRectMake(49, 10, 4, 4));
CGContextFillEllipseInRect(contextRef, CGRectMake(73, 24, 4, 4));

I'd like to then be able to move those drawn dots around (specifically to rotate this). How can I do this, where is drawRect drawn to?

Thanks!

役に立ちましたか?

解決

Unlike the earlier answer posted, I'm thinking you mean that you want to rotate the set of 3 dots, not move those dots around separately.

You want to set the transform property on your UIView subclass:

self.transform = CGAffineTransformMakeRotation(M_PI_2); // pi/2 = 90deg clockwise

As well as being able to rotate your view, you can use the transform to move and scale also. And you can easily animate from one transform to another.

There's lot's of tricky business when setting a view's transform, for example there's significant differences between setting the view's bounds vs its frame, and setting the view's layer's anchor point changes the point of rotation. Read the View Geometry and Coordinate Systems section in the View Programming Guide for iOS.

他のヒント

If I understand correctly, what you want to do is to move (rotate either clockwise or counter-clockwise) the dots that you're drawing, right?

If that's the case, instead of using CoreGraphics to draw the dots what I'd recommend you to do is to create a UIView subclass that knows how to draw one single dot and that it's whole size (width and height) correspond to the diameter of the dot. Also make the background transparent.

After you've done that you can create as many dots as you want and add them as subviews of another containing view. If you do this you'll be able to move the origin of the dots (which really are UIViews) freely by modifying their frame.origin property. More over, being frame one of UIView's properties that gets animated automatically for you, whenever you change the position (origin) of any of your dots the movement will be animated.

Let me know if you need help implementing this.

Hope this helps!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top