UI06_UIPageControl

来源:互联网 发布:西门子工业软件怎么样 编辑:程序博客网 时间:2024/06/03 10:46
////  AppDelegate.h//  UI06_UIPageControl////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end
</pre><pre name="code" class="objc"><pre name="code" class="objc">////  AppDelegate.m//  UI06_UIPageControl////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import "AppDelegate.h"#import "MainViewController.h"@interface AppDelegate ()@end@implementation AppDelegate-(void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    [_window release];    MainViewController *mainVC=[[MainViewController alloc] init];    self.window.rootViewController=mainVC;    [mainVC release];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end


</pre><pre name="code" class="objc"><pre name="code" class="objc">////  MainViewController.h//  UI06_UIPageControl////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface MainViewController : UIViewController@end


</pre><pre name="code" class="objc"><pre name="code" class="objc">////  MainViewController.m//  UI06_UIPageControl////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. 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.    LTview *view=[[LTview alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];    [self.view addSubview:view];    [view release];    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;    for (NSInteger i=1; i<9; i++) {        NSString *picstr=[NSString stringWithFormat:@"%ld.jpg",i];        UIImageView *view=[[UIImageView alloc] initWithImage:[UIImage imageNamed:picstr]];        view.frame=CGRectMake(WIDTH*(i-1), 0, WIDTH, HEIGHT);        [self.scrollView addSubview:view];        [view release];    }    self.scrollView.bounces=NO;    self.scrollView.showsHorizontalScrollIndicator=NO;    self.scrollView.showsVerticalScrollIndicator=NO;    //self.scrollView.    UIPageControl *pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(100, 500, 200, 40)];    [self.view addSubview:pageControl];    pageControl.backgroundColor=[UIColor orangeColor];    [pageControl release];//图片个数和点的个数相同    pageControl.numberOfPages=8;    //点的背景的颜色    pageControl.pageIndicatorTintColor=[UIColor redColor];    //被选中的点的颜色    pageControl.currentPageIndicatorTintColor=[UIColor yellowColor];    //添加点击方法    [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];    self.scrollView.delegate=self;    pageControl.tag=1000;//scrollView的缩放    //缩放的比例    self.scrollView.maximumZoomScale=2;//最大的比例    self.scrollView.minimumZoomScale=0.5;//最小的比例    self.scrollView.zoomScale=1;//原始的缩放比例}//缩放的协议方法的实现//-(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张开始    //触发事件,点击原点,图片切换    [self.scrollView setContentOffset:CGPointMake(WIDTH*page.currentPage, 0) animated:YES];//有动画效果//    self.scrollView.contentOffset=CGPointMake(page.currentPage*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


</pre><pre name="code" class="objc"><pre name="code" class="objc">////  LTview.h//  UI06_UIPageControl////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface LTview : UIView<UITextFieldDelegate>@property(nonatomic,retain)UILabel *label;@property(nonatomic,retain)UITextField *textField;@end


<pre name="code" class="objc">////  LTview.m//  UI06_UIPageControl////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. 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.label=[[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 30)];    [self addSubview:self.label];    [_label release];    self.label.text=@"顶顶顶顶";    self.label.backgroundColor=[UIColor orangeColor];    self.textField=[[UITextField alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];    [self addSubview:self.textField];    [_textField release];    self.textField.backgroundColor=[UIColor lightGrayColor];    self.textField.placeholder=@"请输入";}-(void)dealloc{    [_textField release];    [_label release];    [super dealloc];}@end



0 0