代码之家  ›  专栏  ›  技术社区  ›  Greg Maletic

触摸后保持选中ui按钮

  •  88
  • Greg Maletic  · 技术社区  · 15 年前

    在我的用户单击一个按钮后,我希望在我执行网络操作期间,该按钮保持按下状态。网络操作完成后,我希望按钮返回到其默认状态。

    我试过打电话- [UIButton setSelected:YES] 在按下按钮后(相应地调用- [UIButton setSelected:NO] 在我的网络操作完成后),但它似乎没有任何作用。如果我打电话也一样 setHighlighted: .

    我想我可以尝试交换背景图像来表示网络操作期间的选定状态,但这看起来像是一种黑客行为。有更好的建议吗?

    我的代码如下所示:

    - (IBAction)checkInButtonPushed
    {
        self.checkInButton.enabled = NO;
        self.checkInButton.selected = YES;
        self.checkInButton.highlighted = YES;
        [self.checkInActivityIndicatorView startAnimating];
        [CheckInOperation startWithPlace:self.place delegate:self];
    }
    
    - (void)checkInCompletedWithNewFeedItem:(FeedItem*)newFeedItem wasNewPlace:(BOOL)newPlace possibleError:(NSError*)error;
    {
        [self.checkInActivityIndicatorView stopAnimating];
        self.checkInButton.enabled = YES;
        self.checkInButton.selected = NO;
        self.checkInButton.highlighted = NO;
    }
    
    9 回复  |  直到 8 年前
        1
  •  73
  •   ffizzik    10 年前

    你如何设置不同的图像 UIControlStates 在按钮上?你在为设置背景图像吗? UIControlStateHighlighted 以及 UIControlStateSelected ?

    UIImage *someImage = [UIImage imageNamed:@"SomeResource.png"];
    [button setBackgroundImage:someImage forState:UIControlStateHighlighted];
    [button setBackgroundImage:someImage forState:UIControlStateSelected];
    

    如果您在按钮按下事件上设置选定状态,而不是在内部进行触摸,则按钮实际上将处于突出显示+选定状态,因此您也要设置该状态。

    [button setBackgroundImage:someImage forState:(UIControlStateHighlighted|UIControlStateSelected)];
    

    编辑:

    为了总结我在评论中的评论,并解决你发布的代码……你需要设置你的背景图片 UIControl 说明你在。根据代码段,在网络操作期间,此控件状态将被禁用+选中+突出显示。这意味着你需要这样做:

    [button setBackgroundImage:someImage forState:(UIControlStateDisabled|UIControlStateHighlighted|UIControlStateSelected)];
    

    如果你移除 highlighted = YES ,那么您需要:

    [button setBackgroundImage:someImage forState:(UIControlStateDisabled|UIControlStateSelected)];
    

    拿到照片了吗?

        2
  •  29
  •   Hlung    13 年前

    我有一个更简单的方法。只需使用延迟为0的“PerformSelector”即可执行 [button setHighlighted:YES] . 这将在当前runloop结束后执行重新突出显示。

    - (IBAction)buttonSelected:(UIButton*)sender {
        NSLog(@"selected %@",sender.titleLabel.text);
        [self performSelector:@selector(doHighlight:) withObject:sender afterDelay:0];
    }
    
    - (void)doHighlight:(UIButton*)b {
        [b setHighlighted:YES];
    }
    
        3
  •  27
  •   18446744073709551615    13 年前

    “打开电源,一切都会好起来”

        button.selected = !button.selected;
    

    工作得很好…在我将插座连接到界面生成器中的按钮之后。

    您不需要BackgroundImage:ForState:,生成器允许您指定背景(必要时调整大小)或/和前景(不调整大小)图像。

        4
  •  27
  •   Parth Bhatt    13 年前

    尝试使用nsOperationQueue来实现这一点。试用代码如下:

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        theButton.highlighted = YES;
    }];
    

    希望这有帮助。

        5
  •  8
  •   Bob Spryn    12 年前

    使用一个块,这样就不必构建一个完全独立的方法:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
        theButton.highlighted = YES;
    });
    

    更新

    为了清楚起见,您仍然需要为组合状态设置背景(或正常图像),以及像sbrocket在接受的答案中所说的常规状态。在某些情况下,您的按钮将同时被选中和突出显示,除非您执行以下操作,否则您将无法获得该按钮的图像:

    [button setBackgroundImage:someImage forState (UIControlStateHighlighted|UIControlStateSelected)];
    

    否则,您的按钮可以返回uicontrol状态正常图像以显示短暂的选中+突出显示的状态,您将看到闪光。

        6
  •  2
  •   Eike    8 年前

    在斯威夫特,我是这样做的。

    我创建了 UIButton 实现了一个自定义属性 state

    class ActiveButton: UIButton {
    
        private var _active = false
        var active:Bool {
            set{
                _active = newValue
                updateState()
            }
            get{
                return _active
            }
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.addTarget(self, action: #selector(ActiveButton.touchUpInside(_:)), forControlEvents: .TouchUpInside)
        }
    
        func touchUpInside(sender:UIButton) {
            active = !active
        }
    
        private func updateState() {
            NSOperationQueue.mainQueue().addOperationWithBlock {
                self.highlighted = self.active
            }
        }
    
    }
    

    非常适合我。

        7
  •  1
  •   fredrik alexandresoli    13 年前

    我有一个类似的问题,我想要一个按钮来保持点击后的突出显示。 问题是如果你尝试使用 setHighlighted:YES 在你的内部点击动作,它会在你点击动作后立即重置。 - (IBAction)checkInButtonPushed

    我用这种仪器解决了这个问题

    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval: 0.01
                                             target: self
                                           selector: @selector(selectCurrentIndex)
                                           userInfo: nil
                                            repeats: NO];
    

    然后打电话 设置突出显示:是 从我 selectCurrentIndex 方法。我经常使用 UIButtonTypeRoundedRect 按钮。

        8
  •  0
  •   Pach    13 年前

    我有另一种方法……如果你不想使用图像,并且你想要一个按钮的效果,你可以子类化按钮,这里是我的代码:

    在.h文件中:

    @interface reservasButton : UIButton {
    
    BOOL isPressed;
    }
     @end
    

    在.m文件中:

    #import <QuartzCore/QuartzCore.h>
    
    
     @implementation reservasButton
    
     -(void)setupView {  //This is for Shadow
    
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOpacity = 0.5; 
        self.layer.shadowRadius = 1;    
        self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f); //comment
        //   self.layer.borderWidth = 1;
        self.contentVerticalAlignment   = UIControlContentVerticalAlignmentCenter;
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
        // [self setBackgroundColor:[UIColor whiteColor]];
    
        //  self.opaque = YES;
    
    
    }
    
    -(id)initWithFrame:(CGRect)frame{
        if((self = [super initWithFrame:frame])){
            [self setupView];
        }
    
        return self;
    }
    
    -(id)initWithCoder:(NSCoder *)aDecoder{
        if((self = [super initWithCoder:aDecoder])){
            [self setupView];
        }
    
        return self;
    }
    
    //Here is the important code
    
     -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    
      if (isPressed == FALSE) {
    
          self.contentEdgeInsets = UIEdgeInsetsMake(1.0,1.0,-1.0,-1.0);
          self.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
          self.layer.shadowOpacity = 0.8;
    
          [super touchesBegan:touches withEvent:event];
    
          isPressed = TRUE;
    
         }
         else {
    
             self.contentEdgeInsets = UIEdgeInsetsMake(0.0,0.0,0.0,0.0);
             self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
             self.layer.shadowOpacity = 0.5;
    
             [super touchesEnded:touches withEvent:event];
    
             isPressed = FALSE;
    
         }
    
    
     } `
    
        9
  •  0
  •   t9mike    11 年前

    这里是一个使用上述方法的C/单触(xamarin.ios)实现。它假定您已经设置了突出显示的图像状态,并将选中和选中的突出显示状态配置为使用相同的图像。

    var selected = button.BackgroundImageForState(UIControlState.Highlighted);
    button.SetBackgroundImage(selected, UIControlState.Selected);
    button.SetBackgroundImage(selected, UIControlState.Selected | UIControlState.Highlighted);
    button.TouchUpInside += delegate
    {
        NSTimer.CreateScheduledTimer(TimeSpan.FromMilliseconds(0), delegate
        {
            button.Highlighted = true;
            NSTimer.CreateScheduledTimer(TimeSpan.FromMilliseconds(200), delegate
            {
                button.Highlighted = false;
            });
        });
    };