ios 扑克牌效果轮播_ios 代码 使用UIScrollView实现无限图片轮播效果  即imageView动画代码...

原文链接:http://www.jianshu.com/p/139592807c27

ViewController.h

​-#import

"ViewController.h"

//设置图片个数

-#define zImageCount 3

//设置宽度

-#define zScrollViewSize

(self.scrollView.frame.size)

@interface ViewController

()

//轮播图

@property (weak, nonatomic) IBOutlet UIScrollView

*scrollView;

//页码

@property (weak, nonatomic)

IBOutlet UIPageControl *pageControl;

//定时器

@property (weak,nonatomic)NSTimer

*timer;

@end

ViewController.m

​@implementation

ViewController - (void)viewDidLoad

{

[super viewDidLoad];

//加载轮播图片

[self loadScrollView];

//加载页码

[self

loadPageControl];

//加载定时器

[self

loadTimer];

}

//加载轮播图片

-

(void)loadScrollView

{

for (int i=0; i<

zImageCount; i++)

{

//计算imageView的X的坐标

CGFloat imageViewX =i * zScrollViewSize.width;

//NSLog(@"---%d---%f",zImageCount,imageViewX);

UIImageView

*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(imageViewX,

0, zScrollViewSize.width, zScrollViewSize.height)];

//设置我们的图片

NSString

*imageName=[NSString stringWithFormat:@"mv_d.jpg",i];

//NSLog(@"%@",imageName);

//设置图片名称

imageView.image=[UIImage

imageNamed:imageName];

//添加到轮播控制器中

[self.scrollView addSubview:imageView];

}

//计算imageView的宽度

CGFloat imageViewW=zImageCount

* zScrollViewSize.width;

//NSLog(@"%f",imageViewW);

//给轮播器设置滚动范围

self.scrollView.contentSize=CGSizeMake(imageViewW,

0);

//隐藏滚动条

self.scrollView.showsHorizontalScrollIndicator=NO;

//设置分页

self.scrollView.pagingEnabled=YES;

//设置代理

self.scrollView.delegate=self;

}

//加载页码

- (void)loadPageControl

{

​//设置页面总个数

self.pageControl.numberOfPages=zImageCount;

//设置当前页码

self.pageControl.currentPage=0;

//设置当前页码的颜色

self.pageControl.currentPageIndicatorTintColor=[UIColor

yellowColor];

//设置其他页码的颜色

self.pageControl.pageIndicatorTintColor=[UIColor

grayColor];

}

//加载定时器

-

(void)loadTimer

{

//设置定时器,使其1秒钟切换一次,且不断重复切换(repeats:YES)

self.timer=[NSTimer

scheduledTimerWithTimeInterval:1 target:self

selector:@selector(pageChanged:) userInfo:nil repeats:YES];

//取得主循环

NSRunLoop

*mainLoop=[NSRunLoop mainRunLoop];

//将其添加到运行循环中(监听滚动模式)

[mainLoop

addTimer:self.timer

forMode:NSRunLoopCommonModes];

}

//当页码发生改变的时候调用

-

(void)pageChanged:(id)sender

{

//获取当前页面的索引

NSInteger

currentPage=self.pageControl.currentPage;

//获取偏移量

CGPoint

offset=self.scrollView.contentOffset;

//

if

(currentPage >= zImageCount -

1)

{

//将其设置首张图片的索引

currentPage=0;

//恢复偏移量

offset.x = 0;

//NSLog(@"offset%f",offset.x);

}

else

{

//当前索引+1

currentPage ++;

//设置偏移量

offset.x +=

zScrollViewSize.width;

//NSLog(@"offset.x====%f",offset.x);

}

//设置当前页

self.pageControl.currentPage=currentPage;

//设置偏移后的位置

加上动画过度

[self.scrollView

setContentOffset:offset animated:NO];

}

//根据偏移量获取当前页码

-

(void)scrollViewDidEndDecelerating:(UIScrollView

*)scrollView

{

//获取偏移量

CGPoint

offset=scrollView.contentOffset;

//计算当前页码

NSInteger currentPage=offset.x /

zScrollViewSize.width;

//设置当前页码

self.pageControl.currentPage=currentPage;

}

//设置代理方法,当开始拖拽的时候,让计时器停止

-

(void)scrollViewWillBeginDragging:(UIScrollView

*)scrollView

{

//使定时器失效

[self.timer invalidate];

}

//设置代理方法,当拖拽结束的时候,调用计时器,让其继续自动滚动 -

(void)scrollViewDidEndDragging:(UIScrollView *)scrollView

willDecelerate:(BOOL)decelerate

{

//重新启动定时器

[self

loadTimer];

}​

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值