Mar*_*rry 5 objective-c uibutton
我有一个标题在中间的UIButton.我想在按钮的左侧和右侧显示图像(图标).在左侧,它很容易,我只是设置手动坐标.但是在右侧,当显示旋转并且按钮被放大时,我需要沿"x"轴移动图像.
这该怎么做 ?
小智 9
假设您有一个名为button1的UIButton,您可以执行以下操作:
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
imageView1.center = CGPointMake(25, button1.frame.size.height / 2);
imageView1.image = [UIImage imageNamed:@"ThumbsUp.png"];
[button1 addSubview:imageView1];
[imageView1 release];
imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
imageView2.center = CGPointMake(button1.frame.size.width - 25, button1.frame.size.height / 2);
imageView2.image = [UIImage imageNamed:@"ThumbsDown.png"];
[button1 addSubview:imageView2];
[imageView2 release];
Run Code Online (Sandbox Code Playgroud)
在这里,我们向按钮添加两个图像视图,并使用它们的中心来定位它们.对于imageView1,我们将其设置为距离按钮左侧25个像素.对于imageView2,我们从按钮的宽度中减去25.
现在我们必须设置旋转代码:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
[button1 setFrame:CGRectMake(10, 100, 300, 50)];
} else {
[button1 setFrame:CGRectMake(40, 50, 400, 50)];
}
imageView2.center = CGPointMake(button1.frame.size.width - 25, button1.frame.size.height / 2);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们根据我们是纵向还是横向更改按钮的框架,最后我们设置imageView2的中心以调整不同的框架.我们不必费心使用imageView1,因为它保持不变.