IOS入門-ページコントロール

77075 ワード

ページコントロール
  • UID NavigationController(ナビゲーションコントローラ)
  • ルートビューを追加する
  • UID NavigationControllerを使用してジャンプ
  • を実現します.
  • UICTabBarController(タブナビゲーション)
  • UITNavigationControllerをUAPTabControllerに置く
  • ページジャンプ
  • presenterを使ってジャンプします.
  • disysを使って破棄し、pushを使ってジャンプします.
  • popを使って前のページ
  • に戻ります.
  • ページレイアウト解説
  • ワイドスクリーン制御
  • UIDeviceに拡張機能を追加する
  • UID viceを使用して、スクリーン回転を実現します.
  • UICDeviceとUICereen
  • ジェスチャー
  • UITNavigationController(ナビゲーションコントローラ)
    ルートビューを追加
    //
    //  ViewController.m
    //  NavigationController
    #import "ViewController.h"
    #import "FirstViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //  
        self.view.backgroundColor = [UIColor redColor];
        
    }
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        FirstViewController *vc = [[FirstViewController alloc]init];
        UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
        [nc.navigationBar setTranslucent:NO];
        [self presentViewController:nc animated:YES completion:^{
            
        }];
        
        
    }
    @end
    
    
    UITNavigationControllerを使ってジャンプを実現します.
    //
    //  FirstViewController.m
    //  NavigationController
    //
    //  Created by clz on 2019/9/8.
    //  Copyright © 2019 clz. All rights reserved.
    //
    
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor yellowColor];
        
        self.title = @"First";
      UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"  " style:UIBarButtonSystemItemEdit target:self action:@selector(edit:)];
        
        
        UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [editButton setTitle:@"      " forState:UIControlStateNormal];
        [editButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [editButton setBackgroundColor:[UIColor yellowColor]];
        [editButton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:editButton];
        
        self.navigationItem.rightBarButtonItem = item1;
    }
    
    - (void)edit:(UIBarButtonItem *)sender{
        SecondViewController *secondViewC = [[SecondViewController alloc]init];
        [self.navigationController pushViewController:secondViewC animated:YES];
    }
    @end
    
    
    UITabBarController(タブナビゲーション)
    UITNavigationControllerをUAPTabControllerに入れる.
    //
    //  ViewController.m
    //  UITabBarController
    //
    //  Created by clz on 2019/9/8.
    //  Copyright © 2019 clz. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    #import "ThirdViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        //tabbarController    
        //navigationControlelr
        //     viewcontroller
        FirstViewController *first = [[FirstViewController alloc]init];
        UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:first];
        [nav1.navigationBar setTranslucent:NO];
        nav1.title = @"First";
        
        UIImage *nav1DefImage = [UIImage imageNamed:@"home_discovergray"];
        nav1DefImage = [nav1DefImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        
        UIImage *nav1SelectedImage = [UIImage imageNamed:@"home_discovegreen"];
        nav1SelectedImage = [nav1SelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        nav1.tabBarItem.image = nav1DefImage;
        nav1.tabBarItem.selectedImage = nav1SelectedImage;
        
        
        
        SecondViewController *second = [[SecondViewController alloc]init];
        UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:second];
        nav2.title = @"Second";
        nav2.tabBarItem.image = [UIImage imageNamed:@"home_personalgray"];
        nav2.tabBarItem.selectedImage = [UIImage imageNamed:@"home_personalgreen"];
    
        ThirdViewController *third = [[ThirdViewController alloc]init];
        UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:third];
        nav3.title = @"Third";
        nav3.tabBarItem.image = [UIImage imageNamed:@"home_studygray"];
        nav3.tabBarItem.selectedImage = [UIImage imageNamed:@"home_studygreen"];
    
        
        
        UITabBarController *tabBarControlelr = [[UITabBarController alloc]init];
        tabBarControlelr.viewControllers = @[nav1,nav2,nav3];
        tabBarControlelr.tabBar.translucent = NO;
        
        [[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
        
        [[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
        
        [self presentViewController:tabBarControlelr animated:YES completion:^{
            
        }];
       
    }
    
    
    @end
    
    
    ページジャンプ
    presenterを使ってジャンプします.
    //  ViewController.m
    //       
    #import "ViewController.h"
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor redColor];
        /*
         push present         。
         present dismiss  ,push pop  。
         present      ,push          ,       ,       vc,  vc。
         present             ,push                 
         */
        
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        
        FirstViewController *first = [[FirstViewController alloc]init];
        UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:first];
        
        [self presentViewController:nv animated:YES completion:^{
            
        }];
        
    }
    @end
    
    
    dismisを使って破壊したり、pushを使ってジャンプしたりします.
    //  FirstViewController.m
    //       
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor yellowColor];
        self.title = @"First";
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"  " forState:UIControlStateNormal];
        [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        button.frame = CGRectMake(0, 100, 100, 40);
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setTitle:@"  " forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button1];
        button1.frame = CGRectMake(0, 200, 100, 40);
        [button1 setBackgroundColor:[UIColor whiteColor]];
        [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        
    }
    
    - (void)back:(UIButton *)sender{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    
    
    - (void)push:(UIButton *)sender{
        
        SecondViewController *second = [[SecondViewController alloc]init];
        
        [self.navigationController pushViewController:second animated:YES];
        
    }
    
    @end
    
    
    popを使って前のページに戻ります.
    //  SecondViewController.m
    //       
    
    #import "SecondViewController.h"
    #import "ThirdViewController.h"
    
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor greenColor];
        self.title = @"Second";
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setTitle:@"  " forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button1];
        button1.frame = CGRectMake(0, 200, 100, 40);
        [button1 setBackgroundColor:[UIColor whiteColor]];
        [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button2 setTitle:@"  " forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button2];
        button2.frame = CGRectMake(0, 100, 100, 40);
        [button2 setBackgroundColor:[UIColor whiteColor]];
        [button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    }
    - (void)back:(UIButton *)sender{
    //    [self.navigationController popViewControllerAnimated:YES];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    - (void)push:(UIButton *)sender{
        ThirdViewController *third = [[ThirdViewController alloc]init];
        [self.navigationController pushViewController:third animated:YES];
    }
    
    @end
    
    
    ページレイアウトの解説
    //  FirstViewController.m
    //        
    #import "FirstViewController.h"
    
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor=[UIColor yellowColor];
        
        
        NSLog(@" 2 = %f; 2 = %f",self.view.frame.size.width,self.view.frame.size.height);
        
        NSLog(@"X2 = %f;Y2 = %f",self.view.frame.origin.x,self.view.frame.origin.y);
        
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(0,64, 100, 100);
        view.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:view];
    
    }
    
    
    - (void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        
        NSLog(@" 3 = %f; 3 = %f",self.view.frame.size.width,self.view.frame.size.height);
        
        NSLog(@"X3 = %f;Y3 = %f",self.view.frame.origin.x,self.view.frame.origin.y);
    }
    
    
    
    @end
    
    
    縦横スクリーン制御
    UICDeviceに拡張を追加します.
    //  ViewController.m
    //        
    #import "ViewController.h"
    #import "UIDevice+Direction.h"
    #import "AppDelegate.h"
    
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"  " forState:UIControlStateNormal];
        [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        button.frame = CGRectMake(0, 200, self.view.frame.size.width, 30);
        [button setBackgroundColor:[UIColor redColor]];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    - (void)action:(UIButton *)sender{
        ((AppDelegate *)[UIApplication sharedApplication].delegate).allowRotation = YES;
        [UIDevice setNewOrientation:UIInterfaceOrientationLandscapeRight];
    }
    
    
    @end
    
    
    UIdeviceを使って画面を回転させます.
    //  ViewController.m
    //        
    
    #import "ViewController.h"
    #import "UIDevice+Direction.h"
    #import "AppDelegate.h"
    
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"  " forState:UIControlStateNormal];
        [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        button.frame = CGRectMake(0, 200, self.view.frame.size.width, 30);
        [button setBackgroundColor:[UIColor redColor]];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    - (void)action:(UIButton *)sender{
        ((AppDelegate *)[UIApplication sharedApplication].delegate).allowRotation = YES;
        [UIDevice setNewOrientation:UIInterfaceOrientationLandscapeRight];
    }
    
    
    @end
    
    
    UIDeviceとUICereen
    //  ViewController.m
    //  UIDevice UIScreen
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //UIDevice
        UIDevice *currentDevice = [UIDevice currentDevice];
        NSLog(@"%@",currentDevice.name);
        NSLog(@"%@",currentDevice.model);
        NSLog(@"%@",currentDevice.localizedModel);
        NSLog(@"%@",currentDevice.systemName);
        NSLog(@"%@",currentDevice.systemVersion);
        NSLog(@"%@",currentDevice.identifierForVendor);
        NSLog(@"%d",currentDevice.multitaskingSupported);
        NSLog(@"%ld",(long)currentDevice.userInterfaceIdiom);//        
        
        //UIScreen
        UIScreen *screen =[UIScreen mainScreen];
        NSLog(@"        = %f",screen.brightness);
        screen.brightness = 1;
        NSLog(@"        = %f",screen.brightness);
        NSLog(@"     = %f",screen.bounds.size.width);
        NSLog(@"     = %f",screen.bounds.size.height);
    }
    
    
    @end
    
    
    手振り
    //
    //  ViewController.m
    //      
    //
    //  Created by clz on 2019/9/15.
    //  Copyright © 2019 clz. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"testImage"];
        [self.view addSubview:imageView];
        imageView.frame = CGRectMake(0, 200, 100, 100);
    //    imageView.bounds = CGRectMake(0, 0, 100, 100);
        imageView.center = self.view.center;
        imageView.userInteractionEnabled = YES;
        
        //    
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
        tapGesture.numberOfTapsRequired = 1;
        tapGesture.numberOfTouchesRequired  = 1;
        [imageView addGestureRecognizer:tapGesture];
        
          [super viewDidLoad];
        self.view.backgroundColor = [UIColor redColor];
        //  
    //    self.view
        UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
        leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
        
        
        UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
        rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
        
        [self.view addGestureRecognizer:leftSwipe];
        [self.view addGestureRecognizer:rightSwipe];
    
    //    
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick:)];
        longPressGesture.minimumPressDuration = 1;//    
        [imageView addGestureRecognizer:longPressGesture];
    }
    
    - (void)tapClick:(UITapGestureRecognizer *)sender{
        NSLog(@"  ");
    	}
    	
    - (void)swipeClick:(UISwipeGestureRecognizer *)sender{
        //      
        if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
            NSLog(@"   ");
        }else{
            NSLog(@"   ");
        }
    
    - (void)longPressClick:(UILongPressGestureRecognizer *)sender{
        //    ,       
        if (sender.state == UIGestureRecognizerStateBegan) {
            NSLog(@"  ");
    
        }
    }
    
    
    
    
    @end