iPhone第一节:视图控制器和基本视图

视图控制器和基本视图

1、视图控制器

2、基础控件


A、简单界面

AppDelegate.m

#import "AppDelegate.h"
#import "FirstViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  //最先运行
{
    //[[UIScreen mainScreen] bounds]获取屏幕大小
    //@property(nonatomic) CGRect  bounds;      // default bounds is zero origin, frame size. animatable
    // @interface UIWindow : UIView
    //UIWindow继承自UIView,组合UIScreen,组合UIViewController
    //@property(nonatomic,retain) UIScreen *screen ;  // default is [UIScreen mainScreen]. changing the screen may be an expensive operation and should not be done in performance-sensitive code
    //@property(nonatomic,retain) UIViewController *rootViewController NS_AVAILABLE_IOS(4_0);  // default is nil
    //- (id)initWithFrame:(CGRect)frame;    //UIView的默认初始化,Window继承而来
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //@interface UIScreen : NSObject
    //+ (UIScreen *)mainScreen;  // the device's internal screen
    //@property(nonatomic,readonly) CGRect  bounds;  // Bounds of entire screen in points
    //创建控制器
    FirstViewController *tempVC = [[FirstViewController alloc] init];
    //把控制器赋给主窗体
    //UIWindow类组合UIViewController,UIViewController *rootViewController
    self.window.rootViewController = tempVC;
    //让主窗体显示出来
    //- (void)makeKeyAndVisible;    // convenience. most apps call this to show the main window and also make it key. otherwise use view hidden property
    [self.window makeKeyAndVisible];
    //self指AppDelegate的实例对象,即一个应用程序,self.window是该实例对象的窗体(组合UIWindow)
    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
FirstViewController.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

//@synthesize delegate, delegate2;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

#pragma mark -ScrollView
//    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
//    scrollView.pagingEnabled = YES;
//    scrollView.bounces = NO;
//    for (int i = 0; i < 4; i ++)
//    {
//        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(i * 320, 0, 320, self.view.frame.size.height)];
//        imageV.Image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]];
//        [scrollView addSubview:imageV];
//        //[imageV release];
//    }
//    scrollView.contentSize = CGSizeMake(320 * 4, self.view.frame.size.height);
//    scrollView.delegate = self;
//    [self.view addSubview:scrollView];
    //[scrollView release];
    
    
    
//    UIScrollView * scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
//    scrollView2.pagingEnabled = YES;
//    scrollView2.bounces = NO;
//    for (int i = 0; i < 2; i ++)
//    {
//        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(i * 320, 0, 320, self.view.frame.size.height)];
//        imageV.Image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]];
//        [scrollView2 addSubview:imageV];
//        //[imageV release];
//    }
//    scrollView2.contentSize = CGSizeMake(320 , self.view.frame.size.height * 2);
//    scrollView2.delegate = self;
//    [self.view addSubview:scrollView2];

    
    NSLog(@"loading success");
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"滑动");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"滑动结束");
}

- (void)pressCodeBtn:(id)sender
{
    NSLog(@"你再摁一下试试");
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
    NSLog(@"准备输入");
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"textField变成第一响应者");
}

- (BOOL)textFieldShouldEndEditing:(UITextField *) textField
{
    NSLog(@"textField失去第一响应者");
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"textField失去第一响应者2");
    [textField resignFirstResponder];
    //[self.view endEditing:YES];
}

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"输入内容发生变化");
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *) textField
{
    NSLog(@"内容被清空");
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *) textField
{
    [textField resignFirstResponder];
    NSLog(@"点击Return");
    return YES;
}


//- (void)loadView
//{
//    [super loadView];
//    NSLog(@"loading...");
//}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 50, 50)];
    label.font = [UIFont systemFontOfSize:16.0];
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    label.text = @"用户名";
    [self.view addSubview:label];
#pragma mark -label
//    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 40)];
//    label.font = [UIFont systemFontOfSize:16.0];
//    label.text = @"iOS";
//    [self.view addSubview:label];
    
#pragma mark -Button
    UIButton * codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [codeBtn setShowsTouchWhenHighlighted:YES];
    codeBtn.frame = CGRectMake(50, 150, 50, 50);
    [codeBtn setTitle:@"憋摁我" forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    [codeBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    [codeBtn addTarget:self action:@selector(pressCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:codeBtn];
#pragma mark -view
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 200, 300, 200)];
    //第一种:
    [imageView setImage:[UIImage imageNamed:@"002.jpg"]];
    
    //    //第二种:
    //    UIImageView * imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 400, 300, 200)];
    //    NSString *filePath=[[NSBundle mainBundle] pathForResource:@"002" ofType:@"jpg"];
    //    UIImage *images=[UIImage imageWithContentsOfFile:filePath];
    //    [imageView2 setImage:images];
    //
    //    //第三种:
    //    UIImageView * imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 600, 300, 200)];
    //    NSData *data=[NSData dataWithContentsOfFile:filePath];
    //    UIImage *image2=[UIImage imageWithData:data];
    //    [imageView3 setImage:image2];
    [self.view addSubview:imageView];
    //    [self.view addSubview:imageView2];
    //    [self.view addSubview:imageView3];
    
#pragma mark -textField
    UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.placeholder = @"用户名";
    textField.font = [UIFont systemFontOfSize:16.0];
    textField.textColor = [UIColor redColor];
    //textField.background = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"002" ofType:@"jpg"] ];
    [self.view addSubview:textField];
    textField.delegate = self;
    UITextField * textField2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 50)];
    textField2.textAlignment = NSTextAlignmentLeft;
    textField2.placeholder = @"再摁我试试";
    textField2.font = [UIFont systemFontOfSize:16.0];
    textField2.textColor = [UIColor redColor];
    [self.view addSubview:textField2];
    textField2.delegate = self;
    NSLog(@"即将呈现");
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"已经显示");
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"即将移除");
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"已经移除");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
B、简单练习

viewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark -设置背景
    self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"壁纸1.jpg"] ];
    
//    UIImage * i = [UIImage imageNamed:@"壁纸7.jpg"];
//    self.view.layer.contents = (__bridge id)(i.CGImage);
//    //self.view.layer.backgroundColor = [UIColor clearColor].CGColor;  //背景透明
//    
//    NSString * path = [[NSBundle mainBundle] pathForResource:@"壁纸8" ofType:@"jpg"];
//    UIImage * i2 = [UIImage imageWithContentsOfFile:path];
//    self.view.layer.contents = (id)(i2.CGImage);
    
//    UIImageView * bgview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"壁纸1.jpg"]];
//    bgview.frame = CGRectMake(0, 0, UIScrollView.frame.size.width, scrollView.frame.size.height);
//    [self.view addSubview:bgview];
    
#pragma mark -label
    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 30)];
    lable.font = [UIFont boldSystemFontOfSize:24.0];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.backgroundColor = [UIColor clearColor];
    lable.text = @"iOS开发者账号";
    lable.textColor = [UIColor whiteColor];
    //lable.backgroundColor = [UIColor blackColor];
    lable.numberOfLines = 0;
    [self.view addSubview:lable];
    
    UILabel * lableDate = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 180, 20)];
    lableDate.font = [UIFont boldSystemFontOfSize:16.0];
    lableDate.textAlignment = NSTextAlignmentLeft;
    lableDate.backgroundColor = [UIColor clearColor];
    lableDate.text = [NSString stringWithFormat:@"%@", [NSDate date]];
    lableDate.textColor = [UIColor whiteColor];
    lableDate.numberOfLines = 0;
    [self.view addSubview:lableDate];
    
    UILabel * lable2 = [[UILabel alloc] initWithFrame:CGRectMake(50, 180, 50, 30)];
    lable2.font = [UIFont systemFontOfSize:18.0];
    lable2.textAlignment = NSTextAlignmentLeft;
    lable2.backgroundColor = [UIColor clearColor];
    lable2.text = @"账号";
    lable2.textColor = [UIColor blackColor];
    [self.view addSubview:lable2];
    
    UILabel * lable3 = [[UILabel alloc] initWithFrame:CGRectMake(50, 220, 50, 30)];
    lable3.font = [UIFont systemFontOfSize:18.0];
    lable3.textAlignment = NSTextAlignmentLeft;
    lable3.backgroundColor = [UIColor clearColor];
    lable3.text = @"密码";
    lable3.textColor = [UIColor blackColor];
    [self.view addSubview:lable3];
    
#pragma mark -Button
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setShowsTouchWhenHighlighted:YES];
    btn.frame = CGRectMake(60, 280, 60, 30);
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
    [btn setTitle:@"登录" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn setTitle:@"登录" forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setShowsTouchWhenHighlighted:NO];
    btn2.frame = CGRectMake(200, 280, 60, 30);
    btn2.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
    [btn2 setTitle:@"注册" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn2 setTitle:@"请稍候" forState:UIControlStateSelected];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(wait:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn3 setShowsTouchWhenHighlighted:YES];
    btn3.frame = CGRectMake(160, 90, 100, 50);
    btn3.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
    [btn3 setBackgroundImage:[UIImage imageNamed:@"提示2.png"] forState:UIControlStateNormal];
    [btn3 setBackgroundImage:[UIImage imageNamed:@"提示1.png"] forState:UIControlStateHighlighted];
    [self.view addSubview:btn3];
    [btn3 addTarget:self action:@selector(tips:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark -image
    UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 320, 280, 150)];
    imageV.image = [UIImage imageNamed:@"002.jpg"];
    [self.view addSubview:imageV];
    
    UIImageView * imageV2 = [[UIImageView alloc]initWithFrame:CGRectMake(220, 140, 30, 30)];
    imageV2.image = [UIImage imageNamed:@"cl.jpg"];
    [self.view addSubview:imageV2];
    
#pragma mark -textField
    UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 180, 150, 30)];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.placeholder = @"UserName";
    textField.font = [UIFont systemFontOfSize:18.0];
    textField.textColor = [UIColor blackColor];
    textField.background = [UIImage imageNamed:@"1.png"];
    textField.borderStyle = UITextBorderStyleBezel;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:textField];
    //textField.delegate = self;
    
    UITextField * textField2 = [[UITextField alloc] initWithFrame:CGRectMake(110, 220, 150, 30)];
    textField2.textAlignment = NSTextAlignmentLeft;
    textField2.placeholder = @"PassWord";
    textField2.font = [UIFont systemFontOfSize:18.0];
    textField2.textColor = [UIColor blackColor];
    textField2.background = [UIImage imageNamed:@"line2.png"];
    textField2.secureTextEntry = YES;
    [self.view addSubview:textField2];
    textField2.delegate = self;
    
    
}

- (void)login:(id)sender
{
    NSLog(@"登录成功");
}

- (void)wait:(id)sender
{
    UIButton * btn2 = (UIButton *)sender;
    btn2.selected = YES;
    
//    sleep(2);
//    [btn2 setTitle:@"成功" forState:UIControlStateSelected];
}

- (void)tips:(id)sender
{
    NSLog(@"温馨提示:密码由六位数的某字母组成");
}



//- (BOOL)textField:(UITextField *)textField2 shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if ([string isEqualToString:@"a"])
//    {
//        if (range.location < 6)
//        {
//            NSLog(@"输入正确");
//        }
//        else
//        {
//            NSLog(@"输入错误,请重新输入");
//        }
//    }
//    else
//    {
//        NSLog(@"输入错误,请重新输入");
//    }
//    return YES;
//}

- (BOOL)textFieldShouldReturn:(UITextField *)textField2
{
    if ([textField2.text isEqualToString:@"aaaaaa"])
    {
        NSLog(@"成功");
    }
    [textField2 resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
C、第一章练习

viewController.m

#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize textfield, textfield2;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
#pragma mark -logo
	self.view.backgroundColor = [UIColor blackColor];
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(89, 92, 143, 48)];
    imageView.image = [UIImage imageNamed:@"img_首页Logo.png"];
    [self.view addSubview:imageView];
    
#pragma mark -Username
    textfield = [[UITextField alloc] initWithFrame:CGRectMake(45, 244, 230, 20)];
    textfield.textColor = [UIColor whiteColor];
    textfield.textAlignment = NSTextAlignmentLeft;
    textfield.placeholder = @"用户名";
    textfield.text = @"HuaShan";
    //更改提示的颜色
    [textfield setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    textfield.font = [UIFont systemFontOfSize:16.0];
    textfield.clearButtonMode = UITextFieldViewModeAlways;
    //更改clearButton的图片
    UIButton * clearButton = [textfield valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"btn_首页用户名取消@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:textfield];
    textfield.delegate = self;
    
#pragma mark -line
    UIImageView * imageViewLine1 = [[UIImageView alloc] initWithFrame:CGRectMake(45, 264, 230, 1)];
    imageViewLine1.image = [UIImage imageNamed:@"img_首页输入框底线.png"];
    [self.view addSubview:imageViewLine1];
    
#pragma mark -Password
    textfield2 = [[UITextField alloc] initWithFrame:CGRectMake(45, 291, 230, 20)];
    textfield2.textColor = [UIColor whiteColor];
    textfield2.textAlignment = NSTextAlignmentLeft;
    textfield2.placeholder = @"密码";
    textfield2.text = @"123456";
    [textfield2 setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    textfield2.font = [UIFont systemFontOfSize:16.0];
    textfield2.clearButtonMode = UITextFieldViewModeAlways;
    UIButton * clearButton2 = [textfield2 valueForKey:@"_clearButton"];
    [clearButton2 setImage:[UIImage imageNamed:@"btn_首页用户名取消@2x.png"] forState:UIControlStateNormal];
    //输入保护
    textfield2.secureTextEntry = YES;
    [self.view addSubview:textfield2];
    textfield2.delegate = self;
    
#pragma mark -line2
    UIImageView * imageViewLine2 = [[UIImageView alloc] initWithFrame:CGRectMake(45, 311, 230, 1)];
    imageViewLine2.image = [UIImage imageNamed:@"img_首页输入框底线.png"];
    [self.view addSubview:imageViewLine2];
    
#pragma mark -loginButton
    UIButton * buttonLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonLogin.showsTouchWhenHighlighted = YES;
    buttonLogin.frame = CGRectMake(45, 325, 230, 45);
    [buttonLogin setBackgroundImage:[UIImage imageNamed:@"btn_登录_n.png"] forState:UIControlStateNormal];
    [self.view addSubview:buttonLogin];
    [buttonLogin addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
}

- (void)jump
{
    //首先判断账号密码(我在.h声明了两个文本域)
    if ([textfield.text isEqualToString:@"HuaShan"] && [textfield2.text isEqualToString:@"123456"])
    {
        //包含ViewController2.h并新建一个控制器
        ViewController2 * viewController2 = [[ViewController2 alloc] init];
        //设置跳转方式
        //UIModalTransitionStyleCoverVertical=0, //默认方式,竖向上推
        //UIModalTransitionStyleFlipHorizontal, //水平反转
        //UIModalTransitionStyleCrossDissolve,//隐出隐现
        //UIModalTransitionStylePartialCurl,//部分翻页效果
        viewController2.modalTransitionStyle = 1;
        //跳转
        [self presentViewController:viewController2 animated:YES completion:nil];
        
    }
    else
    {
        NSLog(@"账号或密码错误");
    }
}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
ViewController2.m

#import "ViewController2.h"
#define SCWIDTH self.view.frame.size.width
#define SCHEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ViewController2 ()

@end

@implementation ViewController2

@synthesize page, currentPage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor blackColor];
    
#pragma mark -ScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCWIDTH, SCHEIGHT)];
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    for (int i = 0; i < 4; i++)
    {
#pragma mark -addImage
        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCHEIGHT * i, SCWIDTH, SCHEIGHT)];
        imageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i + 1]];
        
#pragma mark -addLabel
        //使label在每页的相同位置显示
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, SCHEIGHT - 100 + (SCHEIGHT * i), 120, 30)];
        label.text = [NSString stringWithFormat:@"当前页%d/4", i + 1];
        label.font = [UIFont systemFontOfSize:20.0];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor redColor];
        
        [scrollView addSubview:imageV];
        [scrollView addSubview:label];
    }
    scrollView.contentSize = CGSizeMake(SCWIDTH, SCHEIGHT * 4);
    [self.view addSubview:scrollView];
    scrollView.delegate = self;

#pragma mark -buttonBack
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonBack.frame = CGRectMake(220, SCHEIGHT - 50 , 50, 30);
    [buttonBack setTitle:@"返回" forState:UIControlStateNormal];
    [buttonBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonBack setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    [buttonBack addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonBack];
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    UIImageView * imv = [[UIImageView alloc] initWithImage:nil];
//    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, SCHEIGHT - 100, 100, 30)];
//    label.text = [NSString stringWithFormat:@"1/4"];
//    [imv addSubview:label];
//    [scrollView addSubview:imv];
//}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//- (void) scrollViewDidScroll:(UIScrollView *)sender {
//    // 得到每页宽度
//    CGFloat pageWidth = sender.frame.size.width;
//    // 根据当前的x坐标和页宽度计算出当前页数
//    currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
//}

- (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、付费专栏及课程。

余额充值