如何使 UIButton 随 UIScrollView 一起移动

How to make UIButton move with UIScrollView

我有一个 UIScrollView 菜单,它通过激活 UIButton 在视口上方和下方移动。问题是当我按下按钮使 UIScrollView 菜单上下移动时,按钮只停留在一个地方。我希望 UIButtonUIScrollView 菜单激活时在 上方显示 菜单。这是它的样子,"open" 是 UIButton:

这是 viewcontroller.mUIScrollViewUIButton 的动画代码。

#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!");
                         }];
    }
}

我希望 "open" 按钮位于绿色 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!");
                         }];
    }
}