我有一个 UIScrollView
菜单,它通过激活 UIButton
在视口(viewport)上方和下方移动。问题是当我按下按钮使 UIScrollView
菜单上下移动时,按钮只停留在一个地方。我希望 UIButton
在激活时在 UIScrollView
菜单的上方 显示。这是它的样子,“打开”是 UIButton
:
这是 viewcontroller.m
中 UIScrollView
和 UIButton
的动画代码。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];
openMenu.frame = CGRectMake(220, 270, 60, 30);
}
- (IBAction)OpenMenu:(id)sender {
if (draw1 ==0) {
draw1 = 1;
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
scrollView.frame = CGRectMake(0, 1000, 568, 200);
openMenu.frame = CGRectMake(220, 200, 60, 30);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
} else {
draw1 = 0;
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
scrollView.frame = CGRectMake(0, 300, 568, 200);
openMenu.frame = CGRectMake(220, 270, 60, 30);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
}
我希望“打开”按钮位于绿色 UIScrollView
上方(而不是在其上方),我该怎么做?
最佳答案
好的改变openMenu.frame = <frame>
至 openMenu.layer.frame = <frame>
你的代码应该是这样的 设置初始帧添加此方法
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
draw1 = 1;
openMenu.layer.frame = CGRectMake(self.view.center.x - 30, self.view.frame.size.height - 80, 60, 30);
}
- (IBAction)OpenMenu:(id)sender {
if (draw1 ==0) {
draw1 = 1;
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
scrollView.frame = CGRectMake(0, 1000, 568, 200);
openMenu.layer.frame = CGRectMake(self.view.center.x - 30, self.view.frame.size.height - 80, 60, 30);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
} else {
draw1 = 0;
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
scrollView.frame = CGRectMake(0, 300, 568, 200);
openMenu.layer.frame = CGRectMake(self.view.center.x - 30, 270, 60, 30);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
}
关于ios - 如何使 UIButton 随 UIScrollView 一起移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32017550/