Ios 如何移动球
嗨,我如何在iOS上创建移动球Ios 如何移动球,ios,uiview,Ios,Uiview,嗨,我如何在iOS上创建移动球 当程序启动时,我将在屏幕左侧显示球。随后,每次单击ui按钮,如何在同一x轴上将球向右移动 我设法显示了球,但按下ui按钮时如何更新和重新绘制球的位置?目前,每次我按下ui按钮,它都会创建一个新球,而旧球不会被清除 我理解它,因为我重新创建了一个球的新实例。那么我该如何着手解决这个问题呢 这是我的密码 ballView.m @implementation BallView -(id)initWithFrame:(CGRect)frame { self
- 当程序启动时,我将在屏幕左侧显示球。随后,每次单击
,如何在同一x轴上将球向右移动ui按钮
- 我设法显示了球,但按下
ui按钮时如何更新和重新绘制球的位置?目前,每次我按下
,它都会创建一个新球,而旧球不会被清除ui按钮
- 我理解它,因为我重新创建了一个球的新实例。那么我该如何着手解决这个问题呢
这是我的密码 ballView.m
@implementation BallView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
-(void)drawRect:(CGRect)dirtyRect
{
NSLog(@"in drawRect");
// Get the graphics context and clear it
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Draw a solid ball
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextFillEllipseInRect(ctx, dirtyRect);
[self setNeedsDisplay];
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)moveBall
{
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
BallView *ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:ball];
}
MoveBallController.m
@implementation BallView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
-(void)drawRect:(CGRect)dirtyRect
{
NSLog(@"in drawRect");
// Get the graphics context and clear it
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Draw a solid ball
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextFillEllipseInRect(ctx, dirtyRect);
[self setNeedsDisplay];
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)moveBall
{
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
BallView *ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:ball];
}
创建一个
BallView
实例并将其存储在实例变量中。然后单击按钮时,您只需更新球的框架:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
_ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:_ball];
}
- (void)moveBall {
CGRect frame = _ball.frame;
frame.origin.x += 5; // use some appropriate increment
_ball.frame = frame;
}
其中
\u ball
是您的新实例变量。创建一个BallView
实例并将其存储在实例变量中。然后单击按钮时,您只需更新球的框架:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
_ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:_ball];
}
- (void)moveBall {
CGRect frame = _ball.frame;
frame.origin.x += 5; // use some appropriate increment
_ball.frame = frame;
}
其中
\u ball
是您的新实例变量。创建一个BallView
实例并将其存储在实例变量中。然后单击按钮时,您只需更新球的框架:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
_ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:_ball];
}
- (void)moveBall {
CGRect frame = _ball.frame;
frame.origin.x += 5; // use some appropriate increment
_ball.frame = frame;
}
其中
\u ball
是您的新实例变量。创建一个BallView
实例并将其存储在实例变量中。然后单击按钮时,您只需更新球的框架:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect viewFrame = CGRectMake(0, 0, 30, 30);
_ball = [[BallView alloc] initWithFrame:viewFrame];
[[self dotView] addSubview:_ball];
}
- (void)moveBall {
CGRect frame = _ball.frame;
frame.origin.x += 5; // use some appropriate increment
_ball.frame = frame;
}
其中,
\u ball
是您的新实例变量。一般来说,很少有情况需要使用drawRect。正如@rmaddy所演示的,您可以创建一个球,只需更新帧即可移动球的位置。一般来说,很少有情况需要使用drawRect。正如@rmaddy所演示的,您可以创建一个球,只需更新帧即可移动球的位置。一般来说,很少有情况需要使用drawRect。正如@rmaddy所演示的,您可以创建一个球,只需更新帧即可移动球的位置。一般来说,很少有情况需要使用drawRect。正如@rmaddy演示的,您可以创建一个球,只需更新帧即可移动球的位置。