#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
iOS 使用UIScrollView+UIPageControl实现图片自动切换(续)
最新推荐文章于 2019-10-29 20:28:19 发布