3

我的按钮框架图像真的有问题,我想要的是: 1. 当它被选中时,它会有一个复选标记 2. 当视图被关闭然后返回到上一个屏幕时,它仍然会在选择的图像 3. 当我点击所选图像或其他图像时,它会删除复选标记。

我能够做到,但图像设置有问题:

我想要的是这样的:

在此处输入图像描述

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];

但这是我需要的功能:

在此处输入图像描述

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setImage:bgImage forState:UIControlStateSelected];
[myButton setSelected:YES];

这是下面的代码(如果我正在做的事情有问题,请告诉我,谢谢):

- (void)viewDidLoad {
    UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
    // Create view
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];

    int row = 0;
    int column = 0;
    for(int i = 0; i < _thumbs.count; ++i) {

        UIImage *thumb = [_thumbs objectAtIndex:i];
        myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
        [myButton setImage:thumb forState:UIControlStateNormal];
        [myButton addTarget:self 
                     action:@selector(buttonClicked:) 
           forControlEvents:UIControlEventTouchUpInside];
        myButton.tag = i; 
        NSLog(@"%i",i);

        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSInteger selectedImageSlot1 = [prefs integerForKey:@"selected1"];

        if ( selectedImageSlot1 == i){
            [self turnButtonToSelected];
        } 

        [view addSubview:myButton];

        if (column == 6) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }

    [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}

- (void)turnButtonToSelected
{       UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
    [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
    [myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
    //[self turnButtonToSelected];    
}
- (IBAction)buttonClicked:(id)sender {
    myButton = (UIButton *)sender;
    self.selectedImage = [_images objectAtIndex:myButton.tag];

    if (myButton.selected) {

        [sender setSelected:NO];

    }else {

        UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
        [sender setImage:bgImage forState:UIControlStateSelected];
        [sender setSelected:YES];
    }


    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:myButton.tag forKey:@"slot1"];

    [[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:@"selected1"];

    [[NSUserDefaults standardUserDefaults] synchronize];


    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
    [self dismissModalViewControllerAnimated:NO];

}

或者有没有办法在点击时强制启用按钮?

4

3 回答 3

4

I have just tried this on a different approach.

For me it works just fine and achieves your effect.

Let me know if you need further explanation or the entire Xcode project.

- (void)viewDidLoad
{
    [super viewDidLoad];

    //button 1
    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    imageButton.frame = CGRectMake(30, 50, 100, 80);
    UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
    [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
    [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];

    //button 2
    UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    imageButton2.frame = CGRectMake(180, 50, 100, 80);
    UIImage *btnImage2 = [UIImage imageNamed:@"DSC_5355.JPG"];
    [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
    [imageButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:imageButton];
    [self.view addSubview:imageButton2];
}

- (void)buttonClicked:(UIButton*)button
{
    //NSLog(@"Button clicked! %@", button);

    UIImage *checkmarkImage = [UIImage imageNamed:@"check.png"];
    UIImage *empty = nil;

    //NSLog(@"Imageview: %d", button.imageView.tag);
    if (button.imageView.tag == 0)
    {
        [button setImage:checkmarkImage forState:UIControlStateNormal];
        button.imageView.tag = 1;
    }
    else
    {
        [button setImage:empty forState:UIControlStateNormal];
        button.imageView.tag = 0;
    }
}

Best, Chris

Update: I have updated the code. Here is the entire ViewController. Note: I would solve this using coreData to store the current index. The following code is not the best to solve this issue but is based on your initial code!

//
//  SOViewController.m
//  SOImageOverlay
//
//  Created by Chris on 24.07.12.
//

#import "SOViewController.h"

@interface SOViewController ()

@end


#define CHECKMARK_IMAGE @"check.png"

@implementation SOViewController

@synthesize buttons = _buttons;

#pragma mark - Custom getter
- (NSMutableArray*)buttons
{
    if (_buttons == nil)
        _buttons = [[NSMutableArray alloc] init];

    return _buttons;
}


#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];

//    UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
//    UIButton *myButton = 
//    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
//    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
    // Create view
//    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];

    int row = 0;
    int column = 0;
    for(int i = 0; i < 2; ++i) {

        /*UIImage *thumb = [_thumbs objectAtIndex:i];
        myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
        [myButton setImage:thumb forState:UIControlStateNormal];
        [myButton addTarget:self 
                     action:@selector(buttonClicked:) 
           forControlEvents:UIControlEventTouchUpInside];
        myButton.tag = i; 
        NSLog(@"%i",i);*/

        UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i == 0)
        {
            UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
            imageButton.frame = CGRectMake(30, 50, 100, 80);
            [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
        }
        else if (i == 1)
         {
             UIImage *btnImage = [UIImage imageNamed:@"DSC_5355.JPG"];
             imageButton.frame = CGRectMake(180, 50, 100, 80);
             [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
         }

        [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];

        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSInteger selectedImageSlot1 = [prefs integerForKey:@"selected1"];

        if ( selectedImageSlot1 == i){
            [self turnButtonToSelected:imageButton];
        } 

        [self.view addSubview:imageButton];

        //adding current button into buttons array
        [self.buttons addObject:imageButton];

        if (column == 6) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }

    //[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];


//    //button 1
//    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    imageButton.frame = CGRectMake(30, 50, 100, 80);
//    UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
//    [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
//    [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//    
//    //button 2
//    UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
//    imageButton2.frame = CGRectMake(180, 50, 100, 80);
//    UIImage *btnImage2 = [UIImage imageNamed:@"DSC_5355.JPG"];
//    [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
//    [imageButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
//    [self.view addSubview:imageButton];
//    [self.view addSubview:imageButton2];
}

//- (void)buttonClicked:(UIButton*)button
//{
//    //NSLog(@"Button clicked! %@", button);
//    
//    UIImage *checkmarkImage = [UIImage imageNamed:@"check.png"];
//    UIImage *empty = nil;
//    
//    //NSLog(@"Imageview: %d", button.imageView.tag);
//    if (button.imageView.tag == 0)
//    {
//        [button setImage:checkmarkImage forState:UIControlStateNormal];
//        button.imageView.tag = 1;
//    }
//    else
//    {
//        [button setImage:empty forState:UIControlStateNormal];
//        button.imageView.tag = 0;
//    }
//}



// code from stackoverflow
// ------------------------------------------------------------
- (void)turnButtonToSelected:(UIButton*)button
{       
    UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];

    //UIButton *myButton = button; //just to use the copied code
    button.selected = YES;
    [button setImage:bgImage forState:UIControlStateSelected];


//    [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
//    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
//    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
//    [myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
    //[self turnButtonToSelected];    
}

- (IBAction)buttonClicked:(id)sender 
{
    UIButton *myButton = (UIButton *)sender;
    //I have removed this line... due to the fact that I don't want to 
    //implement your arrays.
    //self.selectedImage = [_images objectAtIndex:myButton.tag];

    //deselecting all
    for (UIButton *button in self.buttons)
    {
        button.selected = NO;
        //[button setImage:nil forState:UIControlStateNormal];
    }

    if (myButton.selected) {

        [sender setSelected:NO];

    }else {

        UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
        [sender setImage:bgImage forState:UIControlStateSelected];
        [sender setSelected:YES];
    }


//    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
//    [prefs setInteger:myButton.tag forKey:@"slot1"];


    //getting index of button in array
    NSInteger index = 0;
    NSInteger foundIndex = -1;
    for (UIButton *button in self.buttons)
    {
        if (button == myButton)
        {
            foundIndex = index;
        }
        index++;
    }

    if (foundIndex >= 0)
    {
        [[NSUserDefaults standardUserDefaults] setInteger:foundIndex forKey:@"selected1"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
   // [self dismissModalViewControllerAnimated:NO];

}
// ------------------------------------------------------------


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}



- (IBAction)closeButtonClicked:(UIBarButtonItem *)sender 
{
    [self dismissModalViewControllerAnimated:YES];
}
@end
于 2012-07-24T09:56:32.720 回答
3

尝试这样做。

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:NO];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];**


UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:YES];
[myButton setImage:bgImage forState:UIControlStateSelected];**

在后面的代码中,您将按钮的状态设置为“选中”,但在上面的代码中,您没有。相反,您已禁用该按钮。试试我提供的上面的代码。谢谢。

于 2012-07-25T05:11:15.293 回答
-1

只需在用户单击按钮时将 chekedImage 添加为按钮顶部的子视图

UIImage *button = [UIImage imageNamed:@"cat.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
//添加目标按钮点击


-(void)buttonClicked : (id)sender{
    如果(工具开关){
       [按钮 addSubview:checkedimageView]; // 只是选中的图像图标
    }
    别的{
       [imageview removeFromSuperview];
    }
}

只有当您的所有图像都有两种类型的资源时,您尝试做的事情才会起作用。1) 图像本身 2) 带有复选框的图像作为透明层。

于 2012-07-24T21:13:01.767 回答