UI06_UIPageControl

//
//  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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值