UI_UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了UI_UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3374字,纯文字阅读大概需要5分钟。
内容图文
#import "ViewController.h"
#define IMAGENUMBER 5
#define SIZE self.view.bounds.size
@interface ViewController ()
{
UIScrollView *_scrollerView ;
UIImageView *_leftImageView ;
UIImageView *_centerImageVIew;
UIImageView *_rightImagView;
UIPageControl *_pageCoontroller;
//当前索引下标
NSInteger _curremtIndex;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =[UIColor brownColor];
[self createScrollerView];
}
-(void)createScrollerView
{
_scrollerView =[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:_scrollerView];
//创建左,中,右,三个ImageView视图
_leftImageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0.jpg"]];
_centerImageVIew =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
_rightImagView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
_leftImageView.frame =CGRectMake(0, 20, SIZE.width, SIZE.height);
_centerImageVIew.frame =CGRectMake(SIZE.width, 20, SIZE.width, SIZE.height);
_rightImagView.frame =CGRectMake(SIZE.width*2, 20, SIZE.width, SIZE.height);
//添加
[_scrollerView addSubview:_leftImageView];
[_scrollerView addSubview:_centerImageVIew];
[_scrollerView addSubview:_rightImagView];
_scrollerView.contentSize=CGSizeMake(SIZE.width*3, SIZE.height);
_scrollerView.pagingEnabled=YES;
_scrollerView.delegate=self;
_scrollerView.contentOffset=CGPointMake(SIZE.width, 0);
_scrollerView.showsHorizontalScrollIndicator=NO;
_scrollerView.showsVerticalScrollIndicator=NO;
//页码指示器
_pageCoontroller =[[UIPageControl alloc]initWithFrame:CGRectMake((SIZE.width-300)/2, SIZE.height-10, SIZE.width-50, 10)];
_curremtIndex=0;
_pageCoontroller.numberOfPages=IMAGENUMBER;
_pageCoontroller.backgroundColor=[UIColor clearColor];
_pageCoontroller.pageIndicatorTintColor=[UIColor orangeColor];
_pageCoontroller.currentPageIndicatorTintColor=[UIColor purpleColor];
_pageCoontroller.currentPage=_curremtIndex;
[_pageCoontroller addTarget:self action:@selector(NextImageClicked) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageCoontroller];
}
-(void)NextImageClicked
{
NSInteger pageImage =_pageCoontroller.currentPage;
if (pageImage==_pageCoontroller.currentPage+1) {
pageImage+=1;
}else{
pageImage-=1;
}
_curremtIndex=pageImage;
_scrollerView.contentOffset=CGPointMake(SIZE.width*pageImage, 0);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self loadImage];
[UIView animateWithDuration:0.5 animations:^{
_pageCoontroller.currentPage= _curremtIndex;
}];
}
-(void)loadImage
{
//创建动画效果
CATransition *transaction =[[CATransition alloc]init];
CGPoint offset =_scrollerView.contentOffset;
//向右滚动
if (offset.x>300) {
//动画类型
transaction.type =@"fade";
//通过取余运算获取当前索引下标
_curremtIndex=(_curremtIndex+1)%IMAGENUMBER;
NSString *rightImage =[NSString stringWithFormat:@"%li.jpg",_curremtIndex];
_rightImagView.image=[UIImage imageNamed:rightImage];
}else{//向左滚动
transaction.type=@"fade";
_curremtIndex=(_curremtIndex-1+IMAGENUMBER)%IMAGENUMBER;
NSString *leftImage =[NSString stringWithFormat:@"%li.jpg",_curremtIndex];
_leftImageView.image=[UIImage imageNamed:leftImage];
}
//添加动画效果
[_scrollerView.layer addAnimation:transaction forKey:@"transaction"];
}
原文:http://www.cnblogs.com/LQCQ-Silent/p/4862619.html
内容总结
以上是互联网集市为您收集整理的UI_UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】全部内容,希望文章能够帮你解决UI_UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。