在 UIView 中绘制或移动物体的最佳实践
对于 iPhone 的 Objective-C 新手,我正在做一些实验。
在我让两个方块在屏幕上移动的情况下,我尝试了两种方法,但似乎没有任何区别。但我想可能是图形更重。
两种方法都在计时器上更新:
[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
1- 移动
我尝试的第一种方法是在更新方法中移动方形 UIView 的中心点:
mySquare.center = CGPointMake(mySquare.center.x+0.5, mySquare.center.y+0.5);
2- 绘图
第二种方法是更改方形 UIView 的框架属性并在中重绘视图更新方法:
[mySquare update:CGRectMake(mySquare.frame.origin.x+0.5, mySquare.frame.origin.y+0.5, mySquare.frame.size.width, mySquare.frame.size.height)];
Square UIView中的更新方法:
[self setFrame:rect];
[self setNeedsDisplay];
所以,在移动和绘制之间。
使用 Objective-C 推动事情发展的最佳方式是什么? 什么会带来最好的性能? 最好的做法是什么?
谢谢罗穆
New to Objective-C for iPhone, I'm making some experiment.
In a case where I make 2 squares moving on the screen, I've tried two methods where it doesn't seem to make any difference. But I guess it might be with heavier graphics.
Both Methods are updated on a timer:
[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
1- moving
The first method I tried is moving the center point of the square UIView in an update method:
mySquare.center = CGPointMake(mySquare.center.x+0.5, mySquare.center.y+0.5);
2- drawing
The second method is changing the frame property of the square UIView and redraw the view in an update method:
[mySquare update:CGRectMake(mySquare.frame.origin.x+0.5, mySquare.frame.origin.y+0.5, mySquare.frame.size.width, mySquare.frame.size.height)];
In the Square UIView update method:
[self setFrame:rect];
[self setNeedsDisplay];
So, between moving and drawing.
What would be the best to make things moving with Objective-C?
What will give the best performance?
What would be the best practice?
Thanks
Romu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的方法是使用 Core Animation。查看 MoveMe 示例应用程序。
The right way to do this is to use Core Animation. Take a look at the MoveMe sample application.