Iphone 按钮重叠
我想制作一个应用程序,它的视图可以随机移动,也可以在你触摸屏幕的位置移动。我有以下问题:我制作了一个开始随机动画的按钮,我想制作另一个停止动画的按钮。问题是两个按钮重叠,只有第一个显示在第二个按钮的位置,我按下它,第二个显示在应该在的位置。我不知道我做错了什么,可能是子视图有问题。这是我画按钮的代码Iphone 按钮重叠,iphone,objective-c,ios,xcode,uibutton,Iphone,Objective C,Ios,Xcode,Uibutton,我想制作一个应用程序,它的视图可以随机移动,也可以在你触摸屏幕的位置移动。我有以下问题:我制作了一个开始随机动画的按钮,我想制作另一个停止动画的按钮。问题是两个按钮重叠,只有第一个显示在第二个按钮的位置,我按下它,第二个显示在应该在的位置。我不知道我做错了什么,可能是子视图有问题。这是我画按钮的代码 - (void)drawRect:(CGRect)rect { // Drawing code UIButton *randomMovementButton = [UIButton buttonW
- (void)drawRect:(CGRect)rect {
// Drawing code
UIButton *randomMovementButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[randomMovementButton addTarget:self
action:@selector(animationLoop:finished:context:)
forControlEvents:UIControlEventTouchDown];
[randomMovementButton setTitle:@"Move Random" forState:UIControlStateNormal];
randomMovementButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self addSubview:randomMovementButton];
UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[stopButton addTarget:self
action:@selector(stopAnimation:)
forControlEvents:UIControlEventTouchDown];
[randomMovementButton setTitle:@"Stop Moving Random" forState:UIControlStateNormal];
randomMovementButton.frame = CGRectMake(80.0, 210.0, 200.0, 40.0);
[self addSubview:stopButton];
}
我发现我的错误,把按钮的名称弄错了,并误用了它们。只需为两个按钮使用不同的框架,它们就不会重叠…它们不会重叠。第二个按钮没有边框。你写了这个:
UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[stopButton addTarget:self
action:@selector(stopAnimation:)
forControlEvents:UIControlEventTouchDown];
[randomMovementButton setTitle:@"Stop Moving Random" forState:UIControlStateNormal];
randomMovementButton.frame = CGRectMake(80.0, 210.0, 200.0, 40.0);
[self addSubview:stopButton];
在第三行和第四行,您编写了
随机移动按钮
,而不是停止按钮
。这意味着标题和框架都设置为randomMovementButton,而不是stopButton。此外,第二部分和第一部分上的框架是相同的(都是CGRectMake(80.0、210.0、200.0、40.0)
),这将使它们在修复时重叠。更改其中一个。您能告诉我如何在代码中执行此操作吗?对不起,我一点经验都没有,我正在学习objective-c。多谢各位@芒迪看看你贴的代码。只需更改两个CGRectMake
函数之一中的坐标即可是的,我知道,那是个愚蠢的错误。非常感谢。顺便说一句,你知道如何在当前时间停止所有动画吗@rickyman20I通常不使用uiview动画,但是如果您知道停止一个uiview动画的方法,您可以像这样在所有视图中循环:for(uiview*self.subview中的子视图){[subview stopAnimation];}另外,我创建了stopAnimation方法。我不知道它是否存在。