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

UI06_UIPageControl

2015-08-06 09:07 513 查看
//
//  LTView.h
//  UI06_UIPageControl
//
//  Created by dllo on 15/8/5.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LTView : UIView<UITextFieldDelegate>

@property(nonatomic, retain)UILabel *myLabel;
@property(nonatomic, retain)UITextField *myTextField;
@end
//
//  LTView.m
//  UI06_UIPageControl
//
//  Created by dllo on 15/8/5.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "LTView.h"

@implementation LTView

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createView];
}
return self;
}

- (void)createView
{
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 20, 100, 100)];
self.myLabel.backgroundColor = [UIColor grayColor];
[self addSubview: self.myLabel];
[_myLabel release];

self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 20, 100, 40)];
self.myTextField.backgroundColor = [UIColor lightGrayColor];
[self addSubview:self.myTextField];
self.myTextField.delegate = self;
[_myTextField release];
self.myTextField.layer.borderWidth = 1;
self.myTextField.layer.cornerRadius = 10;
self.myTextField.layer.masksToBounds = YES;

}

- (void)dealloc
{
[_myTextField release];
[_myLabel release];
[super dealloc];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end


//
//  MainViewController.m
//  UI06_UIPageControl
//
//  Created by dllo on 15/8/5.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "MainViewController.h"
#import "LTView.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UIScrollViewDelegate>

@property(nonatomic, retain)UIScrollView *scrollView;

@end

@implementation MainViewController

- (void)dealloc
{
[_scrollView 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 * 8, HEIGHT);
self.scrollView.pagingEnabled = YES;

//    UIImageView *firstImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"07.jpg"]];
//    firstImage.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
//    [self.scrollView addSubview:firstImage];
//    [firstImage release];

for (NSInteger i = 0; i < 8; 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:@"00.jpg"]];
//    lastImage.frame = CGRectMake(WIDTH * 9, 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;

// 创建一个page
UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 600, 200, 40)];
page.backgroundColor = [UIColor magentaColor];
page.alpha = 0.5;
[self.view addSubview:page];
[page release];

// 图片的个数和点的个数相同
page.numberOfPages = 8;
// 点的背景颜色
page.pageIndicatorTintColor = [UIColor cyanColor];
// 被选中的点的颜色
page.currentPageIndicatorTintColor = [UIColor grayColor];

// 给pagecontrol添加点击方法
[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)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
UIPageControl *page = (UIPageControl *)[self.view viewWithTag:1000];
page.currentPage = self.scrollView.contentOffset.x / WIDTH;

}

- (void)pageAction:(UIPageControl *)page
{
// 点的个数从第0张开始计算
NSLog(@"%ld", page.currentPage);
self.scrollView.contentOffset = CGPointMake(page.currentPage * WIDTH, 0);
//[self.scrollView setContentOffset:CGPointMake(page.currentPage * WIDTH, 0) animated:YES];

}

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