当按下该图像视图上的按钮时,如何更改图像视图的图像

时间:2014-01-10 11:13:00

标签: ios iphone objective-c

我是iPhone的新手,我遇到了一个问题。我有一些图像存储在一个数组中。这些图像在按钮上显示为缩略图,这些按钮位于某些图像视图上,以便为这些图像创建边框。现在我想如果任何人按任何按钮然后该边框图像将更改为粗边框图像,以便用户可以理解他按下哪个按钮

imgArray = [[NSArray alloc]initWithObjects:@"Accessories.png",@"Ariel.png",@"Average-Joe.png",@"Baseball-Bracelet.png",@"Basketball-Bracelet.png",@"Beaded-Flower-Charm.png",@"Beautiful-Vibrant-Bracelets.png",@"Believe-Bracelet.png",@"Black-Ribbon-Charm.png",@"Button-Bag.png",@"Cherry-Bomb-Bracelet.png",@"Christmas-Sacks.png",@"Christmas-Tree-Charm.png",@"Christmas-Trees.png",@"Clown.png",@"Cookie-Monster-Bracelet.png",@"Courage-Bracelet.png",@"Doctor.png",@"Dream-Rainbow.png",@"Duck.png",@"Elephant-Charm.png",@"Frozen-Charm-Bracelet.png",nil];

imageIndex = -1;
imgArryCount = [imgArray count];
divCount = imgArryCount/4;
modValCount = imgArryCount%4;
xAxis = 18;
yAxis = 18;
xxAxis = 41;
yyAxis = 41;

bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
        [bgImage setImage:[UIImage imageNamed:@"bgBack960.png"]];
        [self.view addSubview:bgImage];

        scrlVw=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 55, 320, 395)];
        [scrlVw setDelegate:self];
        [self.scrlVw setPagingEnabled:YES];
        [self.view addSubview:scrlVw];

for (int i=1; i<=divCount; i++)
        {

            imgBackVw1 = [[UIImageView alloc]initWithFrame:CGRectMake(18, yAxis, 58, 60)];
            [imgBackVw1 setImage:[UIImage imageNamed:@"image-box960.png"]];
            [self.scrlVw addSubview:imgBackVw1];

            imgBackVw2 = [[UIImageView alloc]initWithFrame:CGRectMake(94, yAxis, 58, 60)];
            [imgBackVw2 setImage:[UIImage imageNamed:@"image-box960.png"]];
            [self.scrlVw addSubview:imgBackVw2];

            imgBackVw3 = [[UIImageView alloc]initWithFrame:CGRectMake(167, yAxis, 58, 60)];
            [imgBackVw3 setImage:[UIImage imageNamed:@"image-box960.png"]];
            [self.scrlVw addSubview:imgBackVw3];

            imgBackVw4 = [[UIImageView alloc]initWithFrame:CGRectMake(240, yAxis, 58, 60)];
            [imgBackVw4 setImage:[UIImage imageNamed:@"image-box960.png"]];
            [self.scrlVw addSubview:imgBackVw4];

            imageIndex=imageIndex+1;
            imageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
            imageBtn1.frame = CGRectMake(5, 5, 48, 50);
            [imageBtn1 setBackgroundImage:[UIImage imageNamed:[imgArray objectAtIndex:imageIndex]] forState:UIControlStateNormal];
            [imageBtn1 addTarget:self action:@selector(imgBtnClckd:) forControlEvents:UIControlEventTouchUpInside];
            [imageBtn1 setTag:imageIndex];
            [imgBackVw1 addSubview:imageBtn1];

            [imgBackVw1 bringSubviewToFront:imageBtn1];
            [imgBackVw1 setUserInteractionEnabled:YES];

            imageIndex=imageIndex+1;
            imageBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
            imageBtn2.frame = CGRectMake(5, 5, 48, 50);
            [imageBtn2 setBackgroundImage:[UIImage imageNamed:[imgArray objectAtIndex:imageIndex]] forState:UIControlStateNormal];
            [imageBtn2 addTarget:self action:@selector(imgBtnClckd:) forControlEvents:UIControlEventTouchUpInside];
            [imageBtn2 setTag:imageIndex];
            [imgBackVw2 addSubview:imageBtn2];
            [changedImgArry2 addObject:imageBtn2];
            [imgBackVw2 bringSubviewToFront:imageBtn2];
            [imgBackVw2 setUserInteractionEnabled:YES];

            imageIndex=imageIndex+1;
            imageBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
            imageBtn3.frame = CGRectMake(5, 5, 48, 50);
            [imageBtn3 setBackgroundImage:[UIImage imageNamed:[imgArray objectAtIndex:imageIndex]] forState:UIControlStateNormal];
            [imageBtn3 addTarget:self action:@selector(imgBtnClckd:) forControlEvents:UIControlEventTouchUpInside];
            [imageBtn3 setTag:imageIndex];
            [imgBackVw3 addSubview:imageBtn3];
            [changedImgArry3 addObject:imageBtn3];
            [imgBackVw3 bringSubviewToFront:imageBtn3];
            [imgBackVw3 setUserInteractionEnabled:YES];

            imageIndex=imageIndex+1;
            imageBtn4 = [UIButton buttonWithType:UIButtonTypeCustom];
            imageBtn4.frame = CGRectMake(5, 5, 48, 50);
            [imageBtn4 setBackgroundImage:[UIImage imageNamed:[imgArray objectAtIndex:imageIndex]] forState:UIControlStateNormal];
            [imageBtn4 addTarget:self action:@selector(imgBtnClckd:) forControlEvents:UIControlEventTouchUpInside];
            [imageBtn4 setTag:imageIndex];
            [imgBackVw4 addSubview:imageBtn4];
            [changedImgArry4 addObject:imageBtn4];
            [imgBackVw4 bringSubviewToFront:imageBtn4];
            [imgBackVw4 setUserInteractionEnabled:YES];

            yAxis=yAxis+78;
        }

        if (modValCount!=0)
        {
            for (int j=0; j<modValCount; j++)
            {
                imgBackVw1 = [[UIImageView alloc]initWithFrame:CGRectMake(xAxis, yAxis, 58, 60)];
                [imgBackVw1 setImage:[UIImage imageNamed:@"image-box960.png"]];
                [changedImgArry1 addObject:imgBackVw1];
                [self.scrlVw addSubview:imgBackVw1];

                imageIndex=imageIndex+1;
                imageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                imageBtn1.frame = CGRectMake(5, 5, 48, 50);
                [imageBtn1 setBackgroundImage:[UIImage imageNamed:[imgArray objectAtIndex:imageIndex]] forState:UIControlStateNormal];
                [imageBtn1 addTarget:self action:@selector(imgBtnClckd:) forControlEvents:UIControlEventTouchUpInside];
                [imageBtn1 setTag:imageIndex];
                [imgBackVw1 addSubview:imageBtn1];
                [imgBackVw1 bringSubviewToFront:imageBtn1];
                [imgBackVw1 setUserInteractionEnabled:YES];
                xAxis=xAxis+78;
                [scrlVw setContentSize:CGSizeMake(320, divCount*78+86)];
            }
        }
        else
        {
            [scrlVw setContentSize:CGSizeMake(320, divCount*78)];
        }

-(void)imgBtnClckd:(UIButton*)sender
{
    NSLog(@"ch...%@",changedImgArry1);

    NSString *str=[imgArray objectAtIndex:sender.tag];
    NSString *imgNameStr = [imgNameAary objectAtIndex:sender.tag];
    detailDesignObj.imgNameGl=str;
    detailDesignObj.getImgNAme = imgNameStr;
    detailDesignObj.getBtnIndex = sender.tag;

    [self.navigationController pushViewController:detailDesignObj animated:YES];
}

任何人都可以帮助我,我该怎么做?

2 个答案:

答案 0 :(得分:0)

你做得很好只需要为touchUpInside状态再添加一个图像就像你为每个按钮为Normalstate做的那样

[imageBtn2 setBackgroundImage:[UIImage imageNamed:[imgArray objectAtIndex:imageIndex]] forState:UIControlStateNormal];

[imageBtn2 setBackgroundImage:[UIImage imageNamed:[imgArray objectAtIndex:imageIndex]] UIControlEventTouchUpInside];

答案 1 :(得分:0)

将此代码放在您的视图上,为imageView的初始图像加载

        imageView1.image = [UIImage imageNamed:@"1.png"];
        imageView2.image = [UIImage imageNamed:@"2.png"];
        imageView3.image = [UIImage imageNamed:@"3.png"];

点击按钮上的代码

-(void)imgBtnClckd:(UIButton*)sender
    {
        imageView1.image = [UIImage imageNamed:@"1.png"];
        imageView2.image = [UIImage imageNamed:@"2.png"];
        imageView3.image = [UIImage imageNamed:@"3.png"];

        if (sender.tag == 0)
        {
            imageView1.image = [UIImage imageNamed:@"darkBorder1.png"];
        }
        else if (sender.tag == 1) 
        {
           imageView2.image = [UIImage imageNamed:@"darkBorder2.png"];
        }
        else if (sender.tag == 2) 
        {
            [imageView3.image = [UIImage imageNamed:@"darkBorder3.png"];
        }
   }