您的位置:首页 > 产品设计 > UI/UE

iOS UI06_UIPageControl

2015-08-06 15:04 471 查看
//
//  MainViewController.m
//  UI06_UIPageControl
//
//  Created by dllo on 15/8/5.
//  Copyright (c) 2015年 zhozhicheng. 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)UIImageView *imageView;
@end

@implementation MainViewController
-(void)dealloc
{
[_scrollView release];
[_imageView release];
[super dealloc];
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
self.scrollView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:self.scrollView];
[self.scrollView release];

self.scrollView.contentSize=CGSizeMake(13 * WIDTH, HEIGHT-100);
self.scrollView.pagingEnabled=YES;

for (NSInteger i=0; i<13; i++) {
NSString *picName=[NSString stringWithFormat:@"cymbal_%02ld.jpg",i];
self.imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];
self.imageView.frame=CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT-100);
[self.scrollView addSubview:self.imageView];
[self.imageView release];

}
self.scrollView.contentOffset=CGPointMake(0, 0);
self.scrollView.bounces=NO;
self.scrollView.showsHorizontalScrollIndicator=NO;
self.scrollView.showsVerticalScrollIndicator=NO;
self.scrollView.delegate=self;

UIPageControl *page =[[UIPageControl alloc] initWithFrame:CGRectMake(100, 580, 200, 40)];
page.backgroundColor=[UIColor redColor];
[self.view addSubview:page];
[page release];
//图片个数和点的个数相同
page.numberOfPages =13;
//点的背景颜色
page.pageIndicatorTintColor=[UIColor blackColor];
//被选中的点的颜色
page.currentPageIndicatorTintColor=[UIColor whiteColor];
//添加点击方法
[page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
page.tag=1000;

//scrollView缩放
//缩放的比例
self.scrollView.maximumZoomScale=2;
self.scrollView.minimumZoomScale=0.5;
//原始缩放比例
self.scrollView.zoomScale=1;

}
#pragma mark 缩放协议方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews firstObject];
}

-(void)pageAction:(UIPageControl *)page
{
//点的个数从0开始
NSLog(@"%ld",page.currentPage);
//触发时,进行图片切换
//    self.scrollView.contentOffset=CGPointMake(WIDTH * (page.currentPage), 0);
[self.scrollView setContentOffset:CGPointMake(WIDTH * (page.currentPage), 0) animated:YES];

}

//
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//滑动图片,让圆点跟着一起动
UIPageControl *page=(UIPageControl *)[self.view viewWithTag:1000];
page.currentPage = self.scrollView.contentOffset.x / WIDTH;
}

- (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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: