可以放大和还原轮播图

来源:互联网 发布:java中什么是实际参数 编辑:程序博客网 时间:2024/04/27 14:00

这是我们的一个作业,做一个可以循环的轮播图, 虽还是有些小问题,但是大体已经做完,可以放大,放大之后可以自动还原。
实现的过程也算简单,在首页添加一个大的ScrollView(_horScrollView),里面是几张图片,点击后进入第二个ViewController中。

在这里可以将 Vertical 看做一个模板,继承 UIView ,这里不再重复赘述。 Vertical模板详见这里

注意:这里的数组是循环的,有两个重复的,除了这个方法,还可以在使用数组的时候进行处理。

MainViewContoller.m

-(void)createScrollView{    self.array = @[@"20",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"20",@"11"];    _horScrollView = [[HorizontalScrollView alloc]initWithFrame:CGRectMake(20, 0, 335,650) imageArray:_array];    _horScrollView.target = self;    _horScrollView.action = @selector(ViewAction:);    _horScrollView.contentSize = CGSizeMake(335, 180 * ((_array.count + 1)/2));    [self.view addSubview:_horScrollView];    [_horScrollView release];}-(void)ViewAction:(UIImageView *)imageView{    NextViewController *next = [[NextViewController alloc]init];    next.tag = imageView.tag;    next.array = [NSArray arrayWithArray:_array];    [self.navigationController pushViewController:next animated:YES];    [next release];}

_horScrollView 为大的ScrollView

此图为首页

HorizontalScrollView.m

-(instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)array{    self = [super initWithFrame:frame];    if (self) {        self.array = [NSMutableArray arrayWithArray:array];        [self initialScrollView];    }    return self;}-(void) initialScrollView{    // 从第一个小 UIScrollView 开始, 从第一张图片开始 , 在垂直的大UIScrollView中,他们的坐标为((i-1)%2, (i-1)/2)        self.contentSize = CGSizeMake(0, self.frame.size.height *((_array.count)/2 -1));    for (int i = 1; i < _array.count - 1; i++) {        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake( 150 * ((i-1)%2)+30, 200 * ((i-1)/2), 120, 180)];        imageView.backgroundColor = [UIColor redColor];     //   NSLog(@"i = %d",i);        NSString *path = [[NSBundle mainBundle]pathForResource:[_array objectAtIndex:i] ofType:@".jpg"];        UIImage *image = [UIImage imageWithContentsOfFile:path];        imageView.image = image;        imageView.tag = i+1;        imageView.userInteractionEnabled = YES;        [self addSubview:imageView];        [imageView release];     //   [image release]; 注意 UImage 类型的不需要释放,否则出现错误    }}// 这是 UIImageView 触摸的方法,当触摸时- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    UITouch *touch = [touches anyObject];    if (touch.view.tag > 0) {        [self.target performSelector:self.action withObject:touch.view];    }}

HorizontalScrollView.h

@property (nonatomic, assign)id target;@property (nonatomic, assign)SEL action;-(instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)array;

第二页的效果图
这里写图片描述

2 0
原创粉丝点击