ios - 如何使 UIButton 随 UIScrollView 一起移动

标签 ios objective-c uiscrollview uibutton

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

enter image description here

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

我希望“打开”按钮位于绿色 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/

相关文章:

ios - 应用内购买订阅

ios - 是否可以在 UIScrollView 的内容末尾添加 View ?

ios - UIScrollView 和 UIPageControl - 在 SWIFT 中的特定页面上启动

cocoa-touch - UISegmentedControl 就像在 AppStore 应用中一样

ios - 如何在 iOS 的 cocoapod 库中使用图像 Assets 目录

ios - 使用属性进行类初始化

ios - ABAddressBookSave 导致 EXC_BAD_ACCESS ARC 已启用

iPhone:在 UIWebView 之间水平滚动以在 URL 历史记录中后退和前进

ios - 如果用户收到远程通知但没有通过 Swift 在 IOS 应用程序上打开它,我该如何检测?

ios - 以编程方式调整 iOS 中的按钮大小