Add 4 image in imageview and change the picture with tap or swipe gesture [closed]

会有一股神秘感。 提交于 2019-12-12 02:36:41

问题


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:


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];


来源:https://stackoverflow.com/questions/10763579/add-4-image-in-imageview-and-change-the-picture-with-tap-or-swipe-gesture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!