可以放大和还原轮播图

这是我们的一个作业,做一个可以循环的轮播图, 虽还是有些小问题,但是大体已经做完,可以放大,放大之后可以自动还原。
实现的过程也算简单,在首页添加一个大的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;

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值