在imageview中添加4张图片,并用点击或滑动手势更改图片 [英] Add 4 image in imageview and change the picture with tap or swipe gesture

查看:58
本文介绍了在imageview中添加4张图片,并用点击或滑动手势更改图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

ToggleImageView.h

ToggleImageView.h

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

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

@end

ToggleImageView.m

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

这篇关于在imageview中添加4张图片,并用点击或滑动手势更改图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆