0

我想在图像视图中添加四个图像。首先,一个图像将是可见的。

每当点击或滑动图像视图时,它都会更改图像。

我想将imageview的图像一一更改4次。

4

1 回答 1

2

ToggleImageView.h

@interface ToggleImageView : UIImageView {
    NSArray *images;
    int currentIndex;
}

- (id)initWithImages:(NSArray *)theImages;

@end

ToggleImageView.m

#import "ToggleImageView.h"

@implementation ToggleImageView

- (id)initWithImages:(NSArray *)theImages {
    self = [self initWithImage:[theImages objectAtIndex:0]];

    if (self) {
        images = [theImages retain];
        currentIndex = 0;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [self addGestureRecognizer:tap];
        [tap release];

        self.userInteractionEnabled = YES;
    }

    return self;
}

-(void)dealloc {
    [images release];

    [super dealloc];
}

- (void)handleTap:(UITapGestureRecognizer *)sender {
    if (images.count > 0) {
        currentIndex++;
        if (currentIndex > images.count - 1) {
            currentIndex = 0;
        }

        self.image = [images objectAtIndex:currentIndex];
    }
}

@end

然后在你的控制器的某个地方

ToggleImageView *tv = [[ToggleImageView alloc] initWithImages:[NSArray arrayWithObjects:[UIImage imageNamed:@"image1"], [UIImage imageNamed:@"image2"], [UIImage imageNamed:@"image3"], [UIImage imageNamed:@"image4"], nil]];
[self.view addSubview:tv];
[tv release];
于 2012-05-26T05:08:01.720 回答