简体   繁体   English

如何移动/动画CGRects

[英]How to move/animate CGRects

I'm having trouble moving CGRects around. 我在移动CGRect时遇到麻烦。 I'm trying to make a breakout game. 我正在尝试制作突破游戏。 In my drawView file, I have written code in the drawRect function by removing the comments. 在我的drawView文件中,我通过删除注释在drawRect函数中编写了代码。 I end my drawRect function with this moveBall(&_ball, &_context, rect); 我用这个moveBall(&_ball, &_context, rect);结束了我的drawRect函数moveBall(&_ball, &_context, rect);

moveball() is declared/implemented in my viewController.h/viewController.m. moveball()在我的viewController.h / viewController.m中声明/实现。

I'm not sure how to then move/animate the ball once it is in my controller. 我不确定一旦球进入控制器后如何移动/动画。 I have the following code: 我有以下代码:

void moveBall(CGRect *ball, CGContextRef *context, CGRect rect) 
{
    CGFloat velX = 1;
    CGFloat velY = 1;

    while (ball->origin.x + CGRectGetWidth(*ball) < CGRectGetWidth(rect))
    {
        CGContextClearRect(*context, *ball);
        *ball = CGRectOffset(*ball, velX, velY);
        CGContextFillEllipseInRect(*context, *ball);
    }
};

What happens is that the ball is "moved" to a different place, not animated there. 发生的事情是,球被“移动”到了另一个位置,而不是在那里动画。 The problem is that the while loop is implemented and the final result is displayed. 问题是实施while循环并显示最终结果。 I want the while loop to run after the drawing is done. 我希望while循环在绘制完成后运行。

Can anyone guide me to make the ball move? 谁能引导我使球移动?

Trying to make animations inside the drawRect method using CoreGraphics framework is exercise in futility. 尝试使用CoreGraphics框架在drawRect方法内制作动画是徒劳的。

For animations you should use other frameworks/technologies intended for that. 对于动画,您应该使用其他用于此目的的框架/技术。

  • Standard apps - Core Animation 标准应用-核心动画
  • Games - SpriteKit, OpenGL ES, Metal, cocos2D, Unity3D, 游戏-SpriteKit,OpenGL ES,Metal,cocos2D,Unity3D,

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

相关问题 如何在Swift中将CGRects相互结合 - How to combine CGRects with each other in Swift 如何在UITextView中有效地查找可见单词的CGRect? - How to efficiently find CGRects for visible words in UITextView? 如何移动/动画 UITextField 占位符文本 - How to move/animate UITextField placeholder text 如何动画(移动)UIButton,让它在移动过程中保持可触摸? - How to animate(move) a UIButton, keeping it touchable during the move? 如何为具有自动布局约束的UILabel移动设置动画? - How do I animate the move a UILabel with auto layout constraints? 当GMSMapView移动时,如何为NSLayoutConstraint设置动画? - How can I animate NSLayoutConstraint when GMSMapView will move? 如何为按钮设置动画以缩小并移动到其他位置? - How do I animate a button to shrink and move to a different location? 如何使用“自动布局”移动标签并为更改设置动画? - How do I move a label and animate the change using Auto Layout? iOS动画&gt;&gt;如何为单个对象设置动画以在多个位置移动 - iOS Animation >> How to animate a single object to move through several positions 您如何像CSS divs一样巧妙地为UIView生成CGRect? - How do you generate CGRects for UIView in a clever way, like CSS divs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM