iOS 使用UIScrollView+UIPageControl实现图片自动切换(续)

#import "adviseredViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface adviseredViewController ()

@end

@implementation adviseredViewController
#define PAGENUM 3
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc
{
    [imageView dealloc];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarHidden:YES ];
    scroll=[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    scroll.contentSize=CGSizeMake(320*3, [[UIScreen mainScreen] bounds].size.height);
    scroll.pagingEnabled=YES;//设置是否分页
    scroll.bounces=NO;
    scroll.delegate=self;
    scroll.showsHorizontalScrollIndicator=NO;//是否显示水平指示条
    scroll.showsVerticalScrollIndicator=NO;
    [self.view addSubview:scroll];
    
    
    for (int i=0; i<3; i++) {
         imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]];
        [scroll addSubview:imageView];       
        [imageView release];
          }
    
    //添加小白点
     page=[[UIPageControl alloc]initWithFrame:CGRectMake(150, 420, 60, 20)];
    page.backgroundColor=[UIColor lightGrayColor];
    //[page setBounds:CGRectMake(0, 0, 60*(page.numberOfPages-1), 30)];
    [page.layer setCornerRadius:8];
    page.numberOfPages=scroll.contentSize.width/320;
    page.currentPage=0;//设置当前页
    page.enabled=YES;//设置分页控件是否可用
    [page addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:page];
    [page release];
    
    timeCount=0;
    //使用NSTimer计时器定时触发滚动控件的滚动效果
    myTimer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollTimer:) userInfo:nil repeats:YES];
    
    // 添加跳过按钮
    UIButton * jumpButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    jumpButton.frame=CGRectMake(250, 420, 60, 30);
    [jumpButton setTitle:@"jump" forState:UIControlStateNormal];
    jumpButton.tintColor=[UIColor redColor];
    [jumpButton addTarget:self action:@selector(comeToHome:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:jumpButton];
    
    //设置scrollview要显示的画面
    [scroll scrollRectToVisible:CGRectMake((page.currentPage)*320, 0, scroll.frame.size.width, scroll.frame.size.height) animated:YES];
    
}

-(void)pageTurn:(id)sender
{
    UIPageControl * pageControl=(UIPageControl *)sender;
    int whichPage=pageControl.currentPage;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5f];
    //设置动画属性变化的曲线
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [scroll setContentOffset:CGPointMake(320*whichPage, 0) animated:YES];
    [UIView commitAnimations];
}

//定时滚动
-(void)scrollTimer:(id)sender
{
    timeCount++;
    if (timeCount==PAGENUM) {
        timeCount=0;
        [scroll scrollRectToVisible:CGRectMake(timeCount*320, 0, 320, [[UIScreen mainScreen]bounds].size.height) animated:YES];
    }
    [scroll scrollRectToVisible:CGRectMake(timeCount*320, 0, 320, [[UIScreen mainScreen] bounds] .size.height) animated:YES];
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger number=scrollView.contentOffset.x/320;
    page.currentPage=number;
    NSLog(@"1111111 %d",page.currentPage);
}

//当滚动视图滚动的时候,视图自动变换的功能取消
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [myTimer invalidate];
}
//当scroll不滚动的时候,视图继续自动切换
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    myTimer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollTimer:) userInfo:nil repeats:YES];
}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本Demo使用UICollectionView实现自动无限轮播功能。 主要功能: 1.实现自动轮播,可修改轮播的时间 2.轮播图片可以来自本地,也可来自网络,通过单独的方法进行设置即可。对于加载网络图片时,Demo中使用了YYWebImage,也可自行替换成SDWebImage。 3.重写了和系统UIPageControl一样的功能,可用图片代替PageControl上的点点,也可自定义其颜色以及切换动画。 使用方法:使用方法比较简单。 /** * 加载本地图片Banner */ - (void)setupLocalBannerImageView { NSArray *array = @[@"1.png", @"2.png", @"3.png", @"4.png", @"5.png"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200) locationImageArray:array]; bannerVew.timeInterval = 2.0; [self.view addSubview:bannerVew]; } /** * 加载网络图片Banner */ - (void)setupNetWorkBannerImageView { NSArray *array = @[@"http://i3.download.fd.pchome.net/t_960x600/g1/M00/07/09/oYYBAFMv8q2IQHunACi90oB0OHIAABbUQAAXO4AKL3q706.jpg", @"http://images.weiphone.net/attachments/photo/Day_120308/118871_91f6133116504086ed1b82e0eb951.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/110425215921926a173e0f728e.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/1104241737046031b3a754f783.jpg"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, 200) netWorkImageArray:array placeHolderImage:nil]; bannerVew.timeInterval = 2.0; bannerVew.pageControlStyle = FFPageControlStyleMiddle; bannerVew.delegate = self; [self.view addSubview:bannerVew]; } 以上方式即可简单使用,如需自定义PageControl也可继承FFAbstractDotView,做些基本的设置即可。 gitHub下载地址:喜欢的朋友请给个星呗! 欢迎各位一起来讨论,有问题请发邮箱270452746@qq.com或者直接加我QQ:270452746进行讨论。谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值