【问题标题】:Move UIButtons when tapped点击时移动 UIButtons
【发布时间】:2014-04-03 19:23:16
【问题描述】:

所以我有一个 UIButtons 的可滚动标签栏,我希望点击的按钮在点击时移动到标签栏的中心。我已经设置好了所有东西,我只是不知道如何移动选定的按钮。

目前我可以在我的方法中指定一个特定的按钮,它会起作用,但我需要它对多个按钮起作用。

@property (strong, nonatomic) IBOutlet UIScrollView *tabBarScrollView;

@property (strong, nonatomic) IBOutlet UIButton *btn_1;
@property (strong, nonatomic) IBOutlet UIButton *btn_2;
@property (strong, nonatomic) IBOutlet UIButton *btn_3;
@property (strong, nonatomic) IBOutlet UIButton *btn_4;
@property (strong, nonatomic) IBOutlet UIButton *btn_5;
@property (strong, nonatomic) IBOutlet UIButton *btn_6;
@property (strong, nonatomic) IBOutlet UIButton *btn_7;
@property (strong, nonatomic) IBOutlet UIButton *btn_8;
@property (strong, nonatomic) IBOutlet UIButton *btn_9;

...

- (void) buttonAnimation{
    [UIView animateWithDuration:(TAB_BAR_ANIMATION_TIME / 2) animations:^{
        CGPoint moveToCenter = CGPointMake(self.btn_1.frame.origin.x +130, 0);
        [self.tabBarScrollView setContentOffset:moveToCenter];
    }];
}

对于我的 CGPoint moveToCenter,什么是更好的数学解决方案?

【问题讨论】:

  • 所以你没有使用滚动视图来启用滚动?
  • @Wain 是的,我这样做了,所有按钮都可以正常工作。我更新了代码以反映标签栏属性。

标签: ios objective-c xcode uibutton cgpoint


【解决方案1】:

您可以在实例化按钮时执行以下操作(针对每个按钮):

[button addTarget:self action:@selector(doSomething:) forControlEvents: UIControlEventTouchUpInside];

和:

-(IBAction)doSomething:(id) sender {
     UIButton *btn = (UIButton *)sender;
    [UIView animateWithDuration:(TAB_BAR_ANIMATION_TIME / 2) animations:^{
        CGPoint moveToCenter = CGPointMake(btn.frame.origin.x +130, 0);
        [self.tabBarScrollView setContentOffset:moveToCenter];
    }];
}

【讨论】:

    【解决方案2】:

    您应该首先将所有 UIButtons 连接到一个 单个 IBAction,然后在按钮上应用动画,该操作被调用:

    这是一个未经测试的代码,可以给你一个想法:

    - (IBAction) buttonAnimation:(id)sender
    {
        CGRect screenFrame  = [[UIScreen mainScreen] bounds];
        UIButton *btn = ((UIButton *)sender).frame;
        CGpoint centerpoint = CGPoint((screenFrame.size.width / 2) - btn.size.width / 2, btn.origin.y); 
        [UIView animateWithDuration:(TAB_BAR_ANIMATION_TIME / 2) 
                         animations:^{
            [self.tabBarScrollView setContentOffset:moveToCenter];
        }];
    }
    

    【讨论】:

    • 是的,我已经在代码中连接了所有内容,我的问题是
    • @HectorLopez,您的陈述不完整。没听懂:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2021-09-09
    • 2020-07-23
    • 2013-03-15
    相关资源
    最近更新 更多