Add 4 image in imageview and change the picture wi

2019-09-01 05:37发布

I want to add four image in image view. at first one image will be visible.

And whenever tap or swipe on image view it will change the image.

i want to change the image of imageview 4 times one by one.

1条回答
beautiful°
2楼-- · 2019-09-01 06:00

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

And then somewhere in your controller

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];
查看更多
登录 后发表回答