【问题标题】:How can I replace the text in a round rect button with a spinner?如何用微调器替换圆形矩形按钮中的文本?
【发布时间】:2012-07-08 02:26:33
【问题描述】:

我有一个带有通用圆形矩形按钮的视图,该按钮可以连接到另一个视图。当用户按下圆角矩形时,它开始获取。在处理提取时,我想用一个旋转的轮子替换圆角矩形按钮内的文本(“下一步”)。

我创建了微调器:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];

但我不知道如何让它替换圆角矩形按钮中的文本。

有什么建议吗?谢谢。

【问题讨论】:

  • 关于如何在这里使用叠加层有什么建议吗?谢谢
  • 与您在任何地方使用它的方式相同——清除文本,然后使覆盖层(以前不可见)可见。

标签: objective-c xcode4 uibutton


【解决方案1】:

像往常一样创建(或获取对)按钮的引用,然后,在处理按钮单击时,创建微调器并将其设置为按钮的子视图。禁用按钮以停止多次点击可能也是值得的。

以下几行应该可以解决问题:

  ...
  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  button.frame = CGRectMake(10, 10, 70, 40);
  [button setTitle:@"Next" forState:UIControlStateNormal];
  [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:button];
  ...

- (void)click:(UIButton *)button {
  [button setTitle:@"" forState:UIControlStateNormal];
  UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  [spinner startAnimating];
  spinner.frame = button.bounds;
  [button addSubview:spinner];
  button.enabled = NO;

  // do your actual work...
}

【讨论】:

    【解决方案2】:

    我写了一个利用关联对象的类别

    UIButton+ActivityIndi​​cator.h

    // Created by reynaldo on 1/16/14.
    
    #import <Foundation/Foundation.h>
    
    @interface UIButton (ActivityIndicator)
    - (void)startActivityIndicator;
    - (void)stopActivityIndicator;
    @end
    

    UIButton+ActivityIndi​​cator.m

    // Created by reynaldo on 1/16/14.
    
    #import <objc/runtime.h>
    #import "UIButton+ActivityIndicator.h"
    
    static char TITLE_KEY;
    static char ACTIVITY_INDICATOR_KEY;
    
    @implementation UIButton (ActivityIndicator)
    
    - (void)startActivityIndicator {
        objc_setAssociatedObject(self, &TITLE_KEY, self.currentTitle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
        [self setTitle:@"" forState:UIControlStateNormal];
        UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        [self addSubview:activityIndicatorView];
        activityIndicatorView.frame = CGRectMake(self.frame.size.width / 2, self.frame.size.height / 2. - 2, 7, 7);
        [activityIndicatorView startAnimating];
    
        objc_setAssociatedObject(self, &ACTIVITY_INDICATOR_KEY, activityIndicatorView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    }
    
    - (void)stopActivityIndicator {
        NSString *title = objc_getAssociatedObject(self, &TITLE_KEY);
        UIActivityIndicatorView *activityIndicatorView = objc_getAssociatedObject(self, &ACTIVITY_INDICATOR_KEY);
    
        if(activityIndicatorView) {
            [activityIndicatorView removeFromSuperview];
        }
    
        if(title.length) {
            [self setTitle:title forState:UIControlStateNormal];
        }
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 1970-01-01
      • 2012-02-14
      • 2011-06-18
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      相关资源
      最近更新 更多