图片轮播功能

#import "ViewController.h"

 

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scorllView;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    for(int i = 0;i<5;i++)

    {

        //实例化对象

        UIImageView *imageView = [[UIImageView alloc]init];

        //设置属性

        CGFloat imageViewX = i*self.scorllView.frame.size.width;

        imageView.frame = CGRectMake(imageViewX, 0, self.scorllView.frame.size.width, self.scorllView.frame.size.height);

        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d",i]];

        

        [self.scorllView addSubview:imageView];

    }

    

    UIImageView *imageView = [self.scorllView.subviews lastObject];

    

    //第一种方式,获取第五张图片的宽

//    self.scorllView.contentSize = CGSizeMake(5*self.scorllView.frame.size.width, 0);

    //第二种方式,先拿到左后一张图片,获取他的x值,

    self.scorllView.contentSize = CGSizeMake(CGRectGetMaxX(imageView.frame), 0);

    

    

    

#pragma mark-----取消滚动条

    self.scorllView.showsHorizontalScrollIndicator = NO;

    self.scorllView.showsVerticalScrollIndicator = NO;

    

    //实现分页效果

    self.scorllView.pagingEnabled = YES;

    //轮播方法

    //设置定时器

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scrollViewToScroll) userInfo:nil repeats:YES];

    

 

}

 

- (void)scrollViewToScroll

{

    //定时方法

    CGPoint offset = self.scorllView.contentOffset;

    

    if(offset.x >= 4*self.scorllView.frame.size.width)

    {

        offset.x = 0;

    }else

    {

        offset.x += self.scorllView.bounds.size.width;

    }

    

    [self.scorllView setContentOffset:offset animated:YES];

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

转载于:https://www.cnblogs.com/qhsblogs/p/5521838.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值