簡體   English   中英

突出顯示的不同圖像 UIButton state

[英]Different images for highlighted UIButton state


對於 UIButton ,我需要2張不同的圖像來突出顯示UIButton

我有這些代碼行:

- (IBAction)buttonPressed:(id)sender
{
    UIImage *followImageHighlighted = [UIImage imageNamed:@"follow-hilite.png"];
    UIImage *unfollowImageHighlighted = [UIImage imageNamed:@"unfollow-hilite.png"];
    if ([sender isSelected]) {
        // set this image for the next time the button will pressed
        [sender setImage:unfollowImageHighlighted forState:UIControlStateHighlighted];
    } else {
        // set this image for the next time the button will pressed
        [sender setImage:followImageHighlighted forState:UIControlStateHighlighted];
    }
}

- (void)viewDidLoad
{
    // ...
    UIImage *followImage = [UIImage imageNamed:@"follow.png"];
    UIImage *unfollowImage = [UIImage imageNamed:@"unfollow.png"];
    [self.followButton setImage:followImage forState:UIControlStateNormal];
    [self.followButton setImage:unfollowImage forState:UIControlStateSelected];
}

問題是每次我按下按鈕時,我都會看到突出顯示的圖像follow-hilite.png

我不能更改道路上按鈕的突出顯示圖像嗎?

I think this is a bad limitation because when the button is selected (thus, "Following") and an user press it, he see the default image, then when it touch up the image is that for selected state and when the.network operation完成后,按鈕圖像會正確切換到所選圖像。

想法?

編輯

- (IBAction)followButtonTapped:(id)sender
{
    BOOL isFollowed = [sender isSelected];
    NSString *urlString = isFollowed ? kUnfollowURL : kFollowURL;
    // operation [...]
    [self.followButton setSelected:(isFollowed) ? NO : YES];
    self.user.followed = !isFollowed;
}

我更好地解釋了這個問題:

  • 默認按鈕 state:白底黑字
  • 所選 state 中的按鈕:黑底白字

如果未關注目標用戶,則該按鈕默認為 state,如果我嘗試按下它,我會看到正確的突出顯示圖像。

但是,如果關注目標用戶並且按鈕位於選定的 state 中,如果我嘗試按下它(並按住手指),我會看到按鈕在白色背景上帶有黑色文本。 這非常丑陋,這是我的問題。

IBAction 是配置控件的尷尬(充其量或不可能)的地方。 您的應用程序中必須有一些條件會觸發對不同突出顯示圖像的要求。 當您檢測到該情況時配置按鈕。

使用“按下”回調來執行應用程序應該在按下時執行的任何操作。

我已經解決了:

[myButton setImage:imageSelectedHover forState:(UIControlStateSelected | UIControlStateHighlighted)];

很高興它有效。 您通過更新應用程序條件解決了它:self.user.followed。 現在,為了讓它真正正確,試試這個:

- (IBAction)followButtonTapped:(id)sender
{
    NSString *urlString = self.user.followed? kUnfollowURL : kFollowURL;
    // operation [...]
    self.user.followed = !self.user.followed;
}

您的 model 的 state 在這里很重要。 按鈕的選定 state 更像是一個布爾值,它位於您保留 state 之后的真實副本的位置附近。

我認為在嘗試修改任何重要內容並計算變量之前,您需要將 sender 轉換為 UIButton* ,因為 sender包含名為-isSelected的方法或屬性。 試試這個:

- (IBAction)buttonPressed:(id)sender
{
    UIImage *followImageHighlighted = [UIImage imageNamed:@"follow-hilite.png"];
    UIImage *unfollowImageHighlighted = [UIImage imageNamed:@"unfollow-hilite.png"];
    if ([self isSelected]) {
        // set this image for the next time the button will pressed
        [(UIButton*)sender setImage:unfollowImageHighlighted forState:UIControlStateHighlighted];
    } else {
        // set this image for the next time the button will pressed
        [(UIButton*)sender setImage:followImageHighlighted forState:UIControlStateHighlighted];
    }
[self isSelected] = ![self isSelected];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM