UI0805_Page
来源:互联网 发布:淘宝网帽子 编辑:程序博客网 时间:2024/06/07 02:30
在滚动屏幕上添加pagecontrol实现点随图动,图随点动,以及自动滚动等
//// MainViewController.m// UI0805_page//// Created by dllo on 15/8/5.// Copyright (c) 2015年 Clare. All rights reserved.//#import "MainViewController.h"#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface MainViewController ()<UIScrollViewDelegate>@property(nonatomic, retain)UIScrollView *scrollView;@property(nonatomic, retain)UIPageControl *page;@end@implementation MainViewController- (void)dealloc{ [_scrollView release]; [_page release]; [super dealloc];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; [self.view addSubview:self.scrollView]; [_scrollView release]; self.scrollView.contentSize = CGSizeMake(WIDTH * 11, HEIGHT); UIImageView *firstImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"09.jpg"]]; firstImage.frame = CGRectMake(0, 0, WIDTH, HEIGHT); [self.scrollView addSubview:firstImage]; [firstImage release]; for (NSInteger i = 1; i < 10; i++) { NSString *picName = [NSString stringWithFormat:@"%02ld.jpg", i]; UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]]; image.frame = CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT); [self.scrollView addSubview:image]; [image release]; } UIImageView *lastImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01.jpg"]]; lastImage.frame = CGRectMake(WIDTH * 10, 0, WIDTH, HEIGHT); [self.scrollView addSubview:lastImage]; [lastImage release]; self.scrollView.bounces = NO; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.delegate = self; self.scrollView.pagingEnabled = YES; self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 600, 200, 40)]; self.page.backgroundColor = [UIColor magentaColor]; self.page.alpha = 1; [self.view addSubview:self.page]; [self.page release]; self.page.numberOfPages = 9; self.page.pageIndicatorTintColor = [UIColor cyanColor]; self.page.currentPageIndicatorTintColor = [UIColor grayColor]; // 手动偏移量 self.scrollView.contentOffset = CGPointMake(WIDTH, 0); [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; // [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(pageAction) userInfo:nil repeats:YES];}- (void)pageAction{ [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0) animated:YES]; if (self.scrollView.contentOffset.x == WIDTH * 9) { self.scrollView.contentOffset = CGPointMake(0, 0); self.page.currentPage = 0; } self.page.currentPage = self.scrollView.contentOffset.x/WIDTH;}- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (self.scrollView.contentOffset.x == 0) { self.scrollView.contentOffset = CGPointMake(WIDTH * 9, 0); } else if (self.scrollView.contentOffset.x == WIDTH *10) { self.scrollView.contentOffset = CGPointMake(WIDTH, 0); } self.page.currentPage = (self.scrollView.contentOffset.x - WIDTH)/WIDTH;}- (void)pageAction:(UIPageControl *)page{ if (self.scrollView.contentOffset.x == 0) { self.scrollView.contentOffset = CGPointMake(WIDTH * 9, 0); } else if (self.scrollView.contentOffset.x == WIDTH *10) { self.scrollView.contentOffset = CGPointMake(WIDTH, 0); } self.scrollView.contentOffset = CGPointMake((page.currentPage + 1 )* WIDTH, 0);}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
0 0