如何使用UIScrollView使UIButton移动

时间:2015-08-14 19:40:18

标签: ios objective-c uiscrollview uibutton

我有一个UIScrollView菜单,可以通过激活UIButton在视口上方和下方移动。问题是,当我按下按钮以使UIScrollView菜单上下移动时,按钮只会停留在一个位置。我希望UIButtonUIScrollView菜单激活时将高于。这是它的样子,“开放”是UIButton

enter image description here

以下是来自viewcontroller.mUIScrollView的{​​{1}}的动画代码。

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!"); }]; } } 上方(不在其上方),我该怎么做?

1 个答案:

答案 0 :(得分:1)

好的,将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!");
                         }];
    }
}