轮播图
#import "ViewController.h"
#import "AppDelegate.h"
#import "NextViewController.h"
@interface ViewController ()
{
UIScrollView *theScroll;
UIPageControl *thePage;
NSArray *theArr;
NSInteger teg;
NSTimer *theTime;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
theScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
float X = 0.0;
for (int i = 0; i <3; i++)
{
UIImageView *theImg = [[UIImageView alloc]initWithFrame:CGRectMake(X, 0, width, height)];
theArr = @[[UIImage imageNamed:@"666.JPG"],[UIImage imageNamed:@"22.JPG"],[UIImage imageNamed:@"33.JPG"]];
theImg.image = theArr[i];
[theScroll addSubview:theImg];
X += width;
}
theScroll.contentSize = CGSizeMake(width * 3, height);
theScroll.pagingEnabled = YES;
theScroll.showsHorizontalScrollIndicator = NO;
theScroll.delegate = self;
thePage = [[UIPageControl alloc]initWithFrame:CGRectMake(width/2-50, 670, 100, 30)];
thePage.backgroundColor = [UIColor clearColor];
thePage.currentPageIndicatorTintColor = [UIColor redColor];
thePage.numberOfPages = 3;
thePage.currentPage = 0;
[self.view addSubview:theScroll];
[self.view addSubview:thePage];
thePage.currentPage = teg;
theTime = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
}
- (void)scroll
{
teg ++;
if (teg >= theArr.count)
{
teg = 0;
}
[theScroll setContentOffset:CGPointMake(teg *self.view.frame.size.width, 0) animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point = theScroll.contentOffset;
thePage.currentPage = point.x/scrollView.frame.size.width;
UIButton *theBtn = [[UIButton alloc]init];
if (thePage.currentPage == 2)
{
[theTime setFireDate:[NSDate distantFuture]];
theBtn.frame = CGRectMake(180, 620, 100, 40);
[theBtn setTitle:@"立即注册" forState:UIControlStateNormal];
[theBtn setTitleColor:[UIColor blueColor] forState: UIControlStateNormal];
[theBtn addTarget:self action:@selector(click) forControlEvents: UIControlEventTouchUpInside ];
[self.view addSubview:theBtn];
}
}
- (void)click
{
NextViewController *next = [[NextViewController alloc]init];
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
app.window.rootViewController = next;
}
@end