如何重置Objective-C中按钮的原始背景图像?

时间:2015-02-02 09:35:26

标签: ios objective-c

我目前正在开发其他人制作的应用。我想改变一些事情,看它是如何运作的。目前,您有一个选择答案的选项,并且无法将其更改为不同的答案。我想改变这一点。我希望用户能够在提交之前更改答案。问题是我不知道名称或我在哪里可以找到按钮的原始图像。有没有办法重置到原来的? 目前,当您将答案按钮拖放到正确的答案按钮上时(我知道它令人困惑。在此应用程序中,您可以拖动一个带有答案按钮图像的按钮,图像会发生变化)。当我尝试更改答案时,图像也会更改,但之前的选择也会保持选中状态。

(抱歉,不知道如何在这里正确发布代码)

- (void) setDefaultImage
{

    // 1) set all the answer button images to unselcted image
    [Ans11 setBackgroundImage:[UIImage imageNamed:@""]         forState:UIControlStateNormal];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans21 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans22 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans23 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans24 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans31 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans32 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans33 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans34 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

}
- (void) buttonDroppedOnAnAnswer:(UIButton *)droppedButton
{
    if(CGRectIntersectsRect(droppedButton.frame, Ans11.frame))
    {
        [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans2.png"]    forState:UIControlStateNormal];
        //[Ans11 setImage:nil];
    }
    else if(CGRectIntersectsRect(droppedButton.frame, Ans12.frame))
    {
        [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
        //[Ans12 setImage:nil];
    }
    else if(CGRectIntersectsRect(droppedButton.frame, Ans13.frame))
    {
        [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans2.png"]    forState:UIControlStateNormal];
        //[Ans13 setImage:nil];
    }
    else if(CGRectIntersectsRect(droppedButton.frame, Ans14.frame))
    {
        [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
        //[Ans14 setImage:nil];
    }
    [droppedButton setUserInteractionEnabled:YES];
}
//Method working when the end of touch is recognized:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setDefaultImage];
    //Assigning the coordinations of the initial position of but1, but2,   but3, but4:
CGPoint position1;
position1.y=226;
position1.x=974;

CGPoint position2;
position2.y=301;
position2.x=974;

CGPoint position3;
position3.y=377;
position3.x=974;

CGPoint position4;
position4.y=458;
position4.x=974;

self.view.multipleTouchEnabled = NO;

 //Long list of conditions, what will happen if each but will intersect with each picture or each answer:
//if but1 will be dropped on Ans11...
if (CGRectIntersectsRect(but1.frame, Ans11.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    //...the backgound of Ans11 will change for ans2.png image:
    [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //User interaction is enabled to allow reuse of the button:
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans12.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //[Ans11 setImage:nil];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans13.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans14.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans21.frame))
{
    [Ans21 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans22.frame))
{
    [Ans22 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans23.frame))
{
    [Ans23 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
编辑:我已经找到了另一种方法来做到这一点。相反,我添加了另一个可拖动的按钮,它只是取消了之前的选择。这是一种更简单的方法,但可能需要更多重复的代码行。非常感谢你的帮助!!

4 个答案:

答案 0 :(得分:0)

假设您将按钮拖放到任何答案时调用以下方法 -

- (void) buttonDroppedOnAnAnswer
{
   // 1) set all the answer button images to unselcted image
   // Suppose we have 4 answer buttons then set
    [Ans11 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];

// check the intersection of button on the answer
// set the intersected answer button image to selected image

   if(CGRectIntersectsRect(but1.frame, Ans11.frame))
   {
      [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans1.png"] forState:UIControlStateNormal];
    //[Ans11 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
   else if(CGRectIntersectsRect(but1.frame, Ans12.frame))
   {
      [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //[Ans12 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
   else if(CGRectIntersectsRect(but1.frame, Ans13.frame))
   {
      [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans3.png"] forState:UIControlStateNormal];
      //[Ans13 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
   else if(CGRectIntersectsRect(but1.frame, Ans14.frame))
   {
      [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans4.png"] forState:UIControlStateNormal];
    //[Ans14 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
}

答案 1 :(得分:0)

如果您想用4个不同的按钮选择4个答案,则应首先将所有答案按钮设置为默认图像(初始化期间)。

- (void) setDeafultImagesOnAnswerButtons
{
// 1) set all the answer button images to unselcted image
   // Suppose we have 4 answer buttons then set
    [Ans11 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
}

然后只需检查答案上的删除按钮的交叉点并设置相应的图像,如下面的函数所示 -

//如果您有多个下拉按钮,则使用相应的删除按钮调用该函数

- (void) buttonDroppedOnAnAnswer:(UIButton *)droppedButton
{
// check the intersection of button on the answer
// set the intersected answer button image to selected image

   if(CGRectIntersectsRect(droppedButton.frame, Ans11.frame))
   {
      [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans1.png"] forState:UIControlStateNormal];
    //[Ans11 setImage:nil];
   }
   else if(CGRectIntersectsRect(droppedButton.frame, Ans12.frame))
   {
      [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //[Ans12 setImage:nil];
   }
   else if(CGRectIntersectsRect(droppedButton.frame, Ans13.frame))
   {
      [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans3.png"] forState:UIControlStateNormal];
      //[Ans13 setImage:nil];
   }
   else if(CGRectIntersectsRect(droppedButton.frame, Ans14.frame))
   {
      [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans4.png"] forState:UIControlStateNormal];
    //[Ans14 setImage:nil];
   }
      [droppedButton setUserInteractionEnabled:YES];
}

答案 2 :(得分:0)

UIButton的backgroundImageForState方法返回UIImage

- (UIImage *)backgroundImageForState:(UIControlState)state

你如何形容:

UIImage *defaultImage = [UIImage imageNamed:@"default.png"];

//假设您最初将按钮的图像设置为 -

UIButton * answerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[answerButton setImage:defaultImage forState:UIControlStateNormal];

//稍后检查

if ([answerButton backgroundImageForState:UIControlStateNormal] == defaultImage) {
    // as the answer button background already contains the default image
    // we need not to set the image
}
else
{
    // as the answer button background contains some other image than the  default image so we need to set the default image to button background
UIImage *defaultImage = [UIImage imageNamed:@"default.png"];
[answerButton setImage:defaultImage forState:UIControlStateNormal];
}

答案 3 :(得分:0)

根据您的要求,我发布了代码 -

首先让我总结一下这些要求 - (如果我错了,请更新我)

有一个视图,您需要向用户显示一个有多个选项的问题,用户可以从这些选项中选择多个选项。

使用按钮显示选项,还有一些按钮用于选择选项。

用于选择答案按钮的按钮被拖放到相应的答案按钮上。

最初,所有答案按钮都将设置为默认图像,并且可拖动按钮也将设置为各自的默认图像。

如果在任何答案按钮上放下任何可拖动按钮,应该将答案按钮设置为某个选定图像,如果除了相应答案按钮之外的其他任何一个答案按钮上还有任何其他可拖动按钮也将被设置对于某些选定的图像等等,所有答案按钮将被设置为选定的图像,因为按钮被放在它们上面。

此外,如果用户点击选定的答案按钮图像,则该答案按钮的图像将设置为默认图像。

例如,我认为有 - 1)4个可拖动按钮 2)12个答案按钮

4个可拖动按钮将设置为各自的默认图像,例如dragButImage1.png,dragButImage2.png,dragButImage3.png,dragButImage4.png。我们还将标记值1,2,3,4添加到这些按钮。

此外,12个答案按钮将设置为各自的默认图像,让我们说ans1.png,ans2.png,ans3.png,ans4.png,ans5.png,ans6.png,ans7.png,ans8。 png,ans9.png,ans10.png,ans11.png,ans12.png。这些按钮的标签值将分别为1,2,3,4,5,6,7,8,9,10,11,12。

现在编码部分

- (void) initializeView
{
// initialize drag-able buttons to default images
    [button1 setTag:1];
    [button1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button1.tag]] forState:UIControlStateNormal];

[button2 setTag:2];
[button2 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button2.tag]] forState:UIControlStateNormal];

[button3 setTag:3];
[button3 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button3.tag]] forState:UIControlStateNormal];

[button4 setTag:4];
[button4 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button4.tag]] forState:UIControlStateNormal];

// initialize anser buttons to default images

[ans1 setTag:1];
[ans1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans1.tag]] forState:UIControlStateNormal];
[ans1 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans2 setTag:2];
[ans2 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans2.tag]] forState:UIControlStateNormal];
[ans2 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans3 setTag:3];
[ans3 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans3.tag]] forState:UIControlStateNormal];
[ans3 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans4 setTag:4];
[ans4 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans4.tag]] forState:UIControlStateNormal];
[ans4 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans5 setTag:5];
[ans5 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans5.tag]] forState:UIControlStateNormal];
[ans5 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans6 setTag:6];
[ans6 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans6.tag]] forState:UIControlStateNormal];
[ans6 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans7 setTag:7];
[ans7 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans7.tag]] forState:UIControlStateNormal];
[ans7 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans8 setTag:8];
[ans8 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans8.tag]] forState:UIControlStateNormal];
[ans8 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans9 setTag:9];
[ans9 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans9.tag]] forState:UIControlStateNormal];
[ans9 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans10 setTag:10];
[ans10 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans10.tag]] forState:UIControlStateNormal];
[ans10 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans11 setTag:11];
[ans11 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans11.tag]]forState:UIControlStateNormal];
[ans11 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans12 setTag:12];
[ans12 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans12.tag]] forState:UIControlStateNormal];
[ans12 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
}

//在答案

上删除可拖动按钮时,将调用以下函数
- (void) aButton:(UIButton *)droppedButton droppedOnAnswer:(UIButton *)answerButton
{
// set the answer button image to selected image
          [answerButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ansSelectedImage%d.png", answerButton.tag]] forState:UIControlStateNormal];

// you could also set the drag-able button image to selected image same as above
[droppedButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"droppedButtonSelectedImage%d.png", droppedButton.tag]] forState:UIControlStateNormal];
}

//触摸任何答案按钮时调用 //此方法将设置触摸的答案按钮的默认图像

-(void) setMyDefaultImage:(UIButton*)answerButton
{
    [answerButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", answerButton.tag]] forState:UIControlStateNormal];
}