UI -知识点回顾 二

来源:互联网 发布:win10edge卸载知乎 编辑:程序博客网 时间:2024/06/05 14:07

1.自定义label—textField视图(复合设计模式)

  //创建LTView继承子UIView

#import<UIKit/UIKit.h>

@interface LTView : UIView

//左侧的Label

@property(strong, nonatomic) UILabel *leftLabel;//strong == retain

//右侧的UITextField

@property(strong,nonatomic) UITextField *rightField;

 

//初始化接口

-(instancetype)initWithFrame:(CGRect)frametitle:(NSString *)title;

 

//设置占位符的方法

-(void)setPlaceHolderString:(NSString*)string;

@end

#import"LTView.h"

@implementation LTView

//利用initWithFrame,去初始化leftlabel和rightFeild

-(instancetype)initWithFrame:(CGRect)frame

{

   self = [super initWithFrame:frame];

   if (self) {

       _leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];

       [self addSubview:_leftLabel];

       _rightField = [[UITextField alloc]initWithFrame:CGRectMake(80, 0, frame.size.width - 80 , 30)];

       [self addSubview:_rightField];

       _rightField.borderStyle = UITextBorderStyleRoundedRect;

   }

   return self;

}

//构造器 初始化接口

-(instancetype)initWithFrame:(CGRect)frametitle:(NSString *)title

{

   self = [self initWithFrame:frame];

   if (self) {

       _leftLabel.text = title;

    }

   return self;

}

 

//设置占位符的方法

-(void)setPlaceHolderString:(NSString *)string

{

    _rightField.placeholder = string;

}

//重写Frame

-(void)setFrame:(CGRect)frame{

   [super setFrame:frame];

   _rightField.frame = CGRectMake(80, 0, frame.size.width-80, 30);

   

}

@end

2.视图控制器

//创建根视图控制器,一个根视图,几个姿势图

//在AppDelegate.m中建立根视图控制器

self.window.rootViewController = [[RootViewControlleralloc]init];

//在根控制器中引入子控制器的头文件

#import "RootviewController.h"

#import "GetBackController.h"

#import"ResignViewController.h"

#import"AppDelegate.h"

//声明子控制器属性

@interfaceRootviewController ()

@property(strong,nonatomic)GetBackController *viewController1;

@property(strong,nonatomic)ResignViewController *viewController2;

@end

@implementationRootviewController

//视图的出现appear (willappear /didappear)

-(void)viewWillAppear:(BOOL)animated

{

   NSLog(@"将要出现%s%d",__FUNCTION__,__LINE__);

}

-(void)viewDidAppear:(BOOL)animated

{

   NSLog(@"视图已出现%s%d",__FUNCTION__,__LINE__);

}

//视图 消失disappear

-(void)viewWillDisappear:(BOOL)animated

{

   NSLog(@"视图正在消失%s%d",__FUNCTION__,__LINE__);

}

-(void)viewDidDisappear:(BOOL)animated

{

   NSLog(@"视图已经消失%s%d",__FUNCTION__,__LINE__);

}

//视图加载完成

- (void)viewDidLoad {

   [super viewDidLoad];

   NSLog(@"视图加载完成%s%d",__FUNCTION__,__LINE__);

   self.view.backgroundColor = [UIColor yellowColor];

   self.viewController1 = [[GetBackController alloc]init];

   self.viewController1.view.backgroundColor = [UIColorredColor];

   self.viewController2 = [[ResignViewController alloc]init];

   self.viewController2.view.backgroundColor = [UIColorgreenColor];

   //添加子视图控制器

  [self addChildViewController:self.viewController1];

  [self addChildViewController:self.viewController2];

//给aButton添加事件

[aButton addTarget:self action:@selector(didClickLoginButton)forControlEvents:UIControlEventTouchUpInside];

}

//页面跳转

//aButton的事件 (模态跳转)

-(void)getPassword:(id)sender{

    NSLog(@"找回密码");

   //模态viewController

   //视图控制器presentViewController

   [self presentViewController:self.viewController1 animated:YES completion:nil];

}

//在子控制器中给Button添加事件

[getBackView.cancelButton addTarget:selfaction:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];

//点击取消按钮返回 的方法dismissViewControllerAnimated:

-(void)cancelButtonAction:(id)sender{

   [self dismissViewControllerAnimated:YES completion:nil];

}

//容器视图控制器

把两个视图控制器都添加为子视图控制器,把两个子视图控制器之间的跳转,必须有相同的的容器视图控制器

//跳转方法(根视图中声明,并实现方法)跳转方法都是成对的,有return有back

-(void)secondVCToThirdVC 

{

    //容器视图控制器 调用transitionFromViewController

   [self transitionFromViewController:self.childViewControllers[0] toViewController:self.childViewControllers[1] duration:2options:UIViewAnimationOptionTransitionCurlUp animations:nil completion:nil];

}

//在子视图控制器中给Button添加跳转方法

-(void)returnThirdView:(id)sender

{    //获取当前容器视图控制器

   FirstViewController *firstVC = (FirstViewController *)self.parentViewController;

   //调用跳转方法

   [firstVC secondVCToThirdVC];

}

 

 

3.检测屏幕旋转的方法

//检测屏幕旋转的方法

-(void)viewWillTransitionToSize:(CGSize)sizewithTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{

   NSLog(@"视图size %@",NSStringFromCGSize(size));

   if (size.width > size.height) {

       //横向

       LTView *nameLTView = (LTView *)[self.view viewWithTag:1001];

       LTView *passwordLTView = (LTView *)[self.view viewWithTag:1002];

       nameLTView.frame = CGRectMake(20, 100, size.width - 40, 30);

        passwordLTView.frame = CGRectMake(20, 150, size.width - 40, 30);

   }else{

       //竖向

       LTView *nameLTView = (LTView *)[self.view viewWithTag:1001];

       LTView *passwordLTView = (LTView *)[self.view viewWithTag:1002];

       nameLTView.frame = CGRectMake(20, 100, size.width - 40, 30);

       passwordLTView.frame = CGRectMake(20, 150, size.width - 40, 30);

 

   }

}

4 、处理内存警告

- (void)didReceiveMemoryWarning {

   [super didReceiveMemoryWarning];

   NSLog(@"收到了内存警告%s%d",__FUNCTION__,__LINE__);

   //将已经展示过得。但未显示的视图,清空

   if (self.view.window == nil &&[self isViewLoaded]) {

       self.view = nil;

   }

}

 

1.Target Action 设计模式

//设置根视图控制器RootViewController,并指定为根控制器

//在根视图中创建三个视图,并添加显示

#import “RootViewController.h"

//引入View接口

#import "TouchView.h"

@interfaceRootViewController ()

 

@end

@implementationRootViewController

//载入视图

- (void)viewDidLoad {

    [super viewDidLoad];

//添加三个视图,分别为改变颜色、位置、控制器的位置

    TouchView *aView = [[TouchViewalloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    aView.backgroundColor = [UIColorredColor];

    [self.view addSubview:aView];

    TouchView *bView = [[TouchViewalloc]initWithFrame:CGRectMake(100, 200, 100, 100)];

    bView.backgroundColor = [UIColorblackColor];

    [self.view addSubview:bView];

    TouchView *cView = [[TouchViewalloc]initWithFrame:CGRectMake(200, 100, 100, 100)];

    cView.backgroundColor = [UIColorgrayColor];

    [self.view addSubview:cView];

 //target action设计模式建立联系  

    [aView addTarget:selfaction:@selector(changeViewColor:)];

    [bView addTarget:selfaction:@selector(changeViewCenter:)];

    [cView addTarget:selfaction:@selector(changeControllerColor:)];

    // Do any additional setup after loading the view.

}

//改变颜色的方法

-(void)changeViewColor:(TouchView *)sender

{

    sender.backgroundColor =[UIColor colorWithRed:arc4random()%256 / 255.0 green:arc4random() %256 / 255.0blue:arc4random()%256 / 255.0 alpha:1];

}

//改变位置的方法

-(void)changeViewCenter:(TouchView *)sender

{

    sender.center =CGPointMake(arc4random()%300 , arc4random()%600);

}

//改变控制器颜色的方法

-(void)changeControllerColor::(TouchView *)sender

{

    self.view.backgroundColor =[UIColor colorWithRed:arc4random()%256/ 255.0 green:arc4random()%256/255.0blue:arc4random()%256/255.0 alpha:1];

}

//内存警告

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

@end

//view.h文件

#import <UIKit/UIKit.h>

/*

 1.声明一个成员变量id型的target

 2.声明另一个成员变量SEL型的action

 3.声明设置target和action的借口addTarge:Action;

 4.在特定情况下调用target执行action;

 **在target执行之前,保证target与action不为空;

 */

@interface TouchView : UIView

{

   id   my_target;

   SEL my_action;

}

-(void)addTarget:(id)target Action:(SEL)action;

@end

 

 

//view.m文件

#import"TouchView.h"

@interface TouchView()

 

@end

@implementation TouchView

-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent *)event

{

   //判断

   if (my_target != nil && my_action != nil) {

       if ([my_target respondsToSelector:my_action]) {

            [my_targetperformSelector:my_action withObject:self];

       }

   }

   

}

//实现设置target,action方法

-(void)addTarget:(id)target Action:(SEL)action

{

   my_target = target;

   my_action = action;

}

@end

//.设置根视图控制器

#import "RootViewController.h"

#import "TouchView.h"

@interfaceRootViewController ()<ChangeStateDelegate>

//4导入文件,遵守协议

@end

@implementation RootViewController

- (void)viewDidLoad {

   [super viewDidLoad];

   //设置三个视图,分别完成点击完成视图变色,移动,控制器变色的功能

   TouchView *touchView = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

   touchView.backgroundColor = [UIColor purpleColor];

   [self.view addSubview:touchView];

   //设置一下代理

   touchView.myDelegate = self;

   touchView.tag = 10010;

    TouchView*touchView2 = [[TouchView alloc]initWithFrame:CGRectMake(200, 100, 100, 100)];

   touchView2.backgroundColor = [UIColor redColor];

   [self.view addSubview:touchView2];

     touchView2.myDelegate = self;

   touchView2.tag = 10011;

   TouchView *touchView3 = [[TouchView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];

   touchView3.backgroundColor = [UIColor blackColor];

   [self.view addSubview:touchView3];

   touchView3.myDelegate = self;

   touchView3.tag = 10012;

   // Do any additional setup after loading the view.

}

//5实现代理方法

-(void)changeTouchViewColor:(TouchView*)touchView

{

   if (touchView.tag == 10010) {

       //tag == 10010 说明改变的是颜色

       touchView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

   }else if (touchView.tag == 10011){

       //tag == 10011 说明改变的是位置

       touchView.center = CGPointMake(arc4random()%(325 - 50 + 1) + 50, arc4random()%(620 - 50 + 1)+ 50);

   }else if (touchView.tag == 10012){

       self.view.backgroundColor = [UIColorcolorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

   }

}

//内存警告

- (void)didReceiveMemoryWarning {

   [super didReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

@end

//视图View.h文件

#import<UIKit/UIKit.h>

@class TouchView;

/*

 1、@protocol声明协议

 2、声明代理delegate的属性

 3、在特定的情况下delegate调用协议方法

 4、作为代理的对象,导入文件,遵守协议

 5、实现协议方法

 */

//1协议声明

@protocol ChangeStateDelegate <NSObject>

//改变touchView的颜色

-(void)changeTouchViewColor:(TouchView*)touchView;

 

@end

@interface TouchView : UIView

//2代理属性

@property(nonatomic,assign) id<ChangeStateDelegate> myDelegate;

 

@end

#import"TouchView.h"

 

@implementation TouchView

//3代理方法的调用

-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent *)event

{

   if (self.myDelegate != nil && [self.myDelegate respondsToSelector:@selector(changeTouchViewColor:)]) {

       [self.myDelegate changeTouchViewColor:self];

   }

}

 

@end

- (void)viewDidLoad {

    [super viewDidLoad];

    //创建方式一

   UIImageView *aView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];

   aView.backgroundColor = [UIColor redColor];

   UIImage *image1 = [UIImage imageNamed:@"12.jpg"];

   aView.image = image1;

   [self.view addSubview:aView];

    //创建方式二

   UIImageView *bView = [[UIImageView alloc]initWithImage:[UIImageimageNamed:@"12.jpg"]];

   bView.frame = CGRectMake(100, 200, 200, 300);

   bView.backgroundColor = [UIColor blueColor];

   [self.view addSubview:bView];

   //****************

   UIImage *imageField = [[UIImage alloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"12" ofType:@"jpg"]];

   UIImageView *cView = [[UIImageView alloc]initWithImage:imageField];

   cView.frame = CGRectMake(1, 100, 375, 375);

   cView.backgroundColor = [UIColor purpleColor];

   [self.view addSubview:cView];

   //填充模式    imageView.contentMode

   cView.contentMode = UIViewContentModeScaleAspectFit;

   //Fill是保持宽高比不变,将视图填满

   //Fit保持宽高比不变,将图片展示完整

   //ToFil 把视图填满,图片被拉伸

   /*UIViewContentModeRedraw,

     UIViewContentModeCenter,

     UIViewContentModeTop,

     UIViewContentModeBottom,

     UIViewContentModeLeft,

     UIViewContentModeRight,

     UIViewContentModeTopLeft,

     UIViewContentModeTopRight,

     UIViewContentModeBottomLeft,

     UIViewContentModeBottomRight,

     */

  //超出部分是否剪切

   cView.clipsToBounds = YES;

   //输出本本应用的Bundle路径

   NSLog(@"%@",[[NSBundle mainBundle]pathForResource:@"12" ofType:@"jpg"]);

   //此种方式创建的图像,是从文件读取的,会按照对象方式释放内存

   //使用ImageNamed方式得到的图像对象。图像对象会被释放后,图片存在缓存区

 }

- (void)viewDidLoad {

    [super viewDidLoad];

    UIImageView *aView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"12.jpg"]];

    aView.frame = CGRectMake(100, 100, 200, 200);

    aView.backgroundColor = [UIColorredColor];

    [self.view addSubview:aView];

    //手势    UIGestureRecognizer

  //创建一个手势 initwithTarge:Action

   //配置手势

   //手势添加到视图上

   //实现Action

   //创建一个轻拍手势

   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]initWithTarget:self action:@selector(tapGestureAction:)];

   //配置手势

     //双击触发轻拍

   tapGesture.numberOfTapsRequired = 2;

   tapGesture.numberOfTouchesRequires = 2;//2根手指

   //手势添加到视图上

   [aView addGestureRecognizer:tapGesture];

    //允许交互

   aView.userInteractionEnabled = YES;

   //创建长按手势

   UILongPressGestureRecognizer *longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];

   //配置长按手势 触发的时间 是否允许手指移动

   longPressGesture.minimumPressDuration = 3; //3s才能触发

   longPressGesture.allowableMovement = 300;

   [aView addGestureRecognizer:longPressGesture];

   aView.userInteractionEnabled = YES;

   //创建一个清扫手势

   UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizeralloc]initWithTarget:self action:@selector(swipeGestureAc:)];

   //清扫手势主要是控制的是滑动的方向

   swipeGesture.direction = UISwipeGestureRecognizerDirectionUp; //上 下 左 右

   [aView addGestureRecognizer:swipeGesture];

   aView.userInteractionEnabled = YES;

   //平移手势

   UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:self action:@selector(panGestureAction:)];

   [aView addGestureRecognizer:panGesture];

   aView.userInteractionEnabled = YES;

   //旋转手势

   UIRotationGestureRecognizer *rotationGesture =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureAction:)];

   [aView addGestureRecognizer:rotationGesture];

   aView.userInteractionEnabled = YES;

   //捏合手势

   UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizeralloc]initWithTarget:self action:@selector(pinchGestureActon:)];

   [self.view addGestureRecognizer:pinchGesture];

   aView.userInteractionEnabled = YES;

}

  //实现轻拍手势

-(void)tapGestureAction:(UITapGestureRecognizer*)tapGesture

{

   NSLog(@"%s %d",__FUNCTION__,__LINE__);

   CGPoint point = [tapGesture locationInView:self.view];

   NSLog(@"%@",NSStringFromCGPoint(point));

}

  //实现长按手势

-(void)longPressGesture:(UILongPressGestureRecognizer*)longPressGesture

{

   NSLog(@"%s %d",__FUNCTION__,__LINE__);

   NSLog(@"%ld",longPressGesture.state);

}

  //实现清扫手势

-(void)swipeGestureAc:(UISwipeGestureRecognizer*)swipeGestue

{

   NSLog(@"%s %d",__FUNCTION__,__LINE__);

   //1.transform旋转view

   swipeGestue.view.transform = CGAffineTransformMakeRotation(M_PI);

   //2.transform缩放view

   swipeGestue.view.transform = CGAffineTransformMakeScale(0.2, 0.3);

   //3.transform位移view

   swipeGestue.view.transform = CGAffineTransformMakeTranslation(100, 100);

}

  // 实现捏合手势

-(void)panGestureAction:(UIPanGestureRecognizer*)panGesture

{

   NSLog(@"平移%s%d",__FUNCTION__,__LINE__);

}

  //实现旋转手势

-(void)rotationGestureAction:(UIRotationGestureRecognizer*)rotationGesture{

   NSLog(@"旋转%s%d",__FUNCTION__,__LINE__);

   NSLog(@"%.2f",rotationGesture.rotation);

   //改变视图角度

   //   rotationGesture.view.transform =CGAffineTransformMakeRotation(rotationGesture.rotation);

   //    rotationGesture.rotation= 0;

   //实现旋转的第二种格式

   rotationGesture.view.transform =CGAffineTransformRotate(rotationGesture.view.transform,rotationGesture.rotation);

}

 

-(void)pinchGestureActon:(UIPinchGestureRecognizer*)pinchGestur

{

   NSLog(@"捏合手势%s%d",__FUNCTION__,__LINE__);

   NSLog(@"%.2f",pinchGestur.scale);

   //   pinchGestur.view.transform =CGAffineTransformMakeScale(pinchGestur.scale, pinchGestur.scale);

   pinchGestur.view.transform =CGAffineTransformScale(pinchGestur.view.transform, pinchGestur.scale,pinchGestur.scale);

   pinchGestur.scale = 1;

   

}

 

UISegmentController

@interfaceRootViewController ()<UIAlertViewDelegate>

@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,assign) intnumber;

@end

@implementation RootViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    NSArray *items = @[@"点击", @"捏合", @"旋转", @"清扫", @"平移", @"边缘移动", @"长按"];

   

   //创建控制器对象

   UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:items];

   //initWithItems:  独有的初始化方法

   //setTitle:forSegmentAtIndex: //为指定的下标的分段设置title

   //selectedSegmentAiindex = 0; 被选中的的segment

   segmentedControl.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);

   segmentedControl.tintColor = [UIColor purpleColor];

   //关联方法

   [segmentedControl addTarget:self action:@selector(changeGesture:)forControlEvents:UIControlEventValueChanged];

   self.imageView = [[UIImageView alloc]initWithImage:[UIImageimageNamed:@"1.jpg"]];

   

   //UIImage *image0 = [[UIImagealloc]initWithContentsOfFile:@"/Users/lanou3g/Desktop/前期课程阶段/UI/未命名文件夹/uisegmentControl/uisegmentControl/1.jpg"];

   //输出本应用的Bundle路径

   NSLog(@"%@",[[NSBundle mainBundle]pathForResource:@"1" ofType:@"jpg"]);

   //UIImage *image1 = [[UIImage alloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"1" ofType:@"jpg"]];

   //此种方式创建的图像,是从文件读取的,会按照对象方式释放

   //用imageName:方式取得图像,图像对象会被释放后,图片存在缓存区

   

   

   self.imageView.frame = CGRectMake(10, 100, 355, 400);

   self.imageView.userInteractionEnabled = YES;

  

   //添加父视图

 

   [self.view addSubview:segmentedControl];

   // Do any additional setup after loading the view.

}

 

 

- (void)changeGesture:(UISegmentedControl*)aControl {

   //选中的是第几个区域

   //selectedSegmentIndex, 从0开始

   NSLog(@"%ld", aControl.selectedSegmentIndex);

   

   [self.view addSubview:self.imageView];

   //移除imageView上的手势(每执行一次case,就要移除一次, 从而使得每一个case只有一个功能)

   for (UIGestureRecognizer *gesture in self.imageView.gestureRecognizers) {

       [self.imageView removeGestureRecognizer:gesture];

   }

   

   //添加手势

   switch (aControl.selectedSegmentIndex) {

       case 0://点击手势

       {

            self.imageView.image = [UIImage imageNamed:@"1.jpg"];//三张图片分别起名为1,2, 3

            //设置图片的填充方式

            self.imageView.contentMode =UIViewContentModeScaleAspectFit;

            //Fill是保持宽高比不变,将视图填满

            //Fit保持宽高比不变,将图片展示完整

            //ToFil把视图填满,图片被拉伸

            /*UIViewContentModeRedraw,

             UIViewContentModeCenter,

             UIViewContentModeTop,

             UIViewContentModeBottom,

             UIViewContentModeLeft,

             UIViewContentModeRight,

             UIViewContentModeTopLeft,

             UIViewContentModeTopRight,

             UIViewContentModeBottomLeft,

             UIViewContentModeBottomRight,

             */

 

            //图片初始值设置为1(number为图片名字)

            self.number = 1;

           

            UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];//tap为关联的方法

            //双击触发tap

            tap.numberOfTapsRequired = 2;

            //点击的触点个数

            tap.numberOfTouchesRequired = 2;

            //把手势添加到某个视图上

            [self.imageView addGestureRecognizer:tap];

            //释放

           

       }

            break;

       case 1://捏合

       {

            UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];//pinch:为关联的有参方法,:不能省略

            [self.imageView addGestureRecognizer:pinch];

                    }

            break;

       case 2://旋转

       {

            UIRotationGestureRecognizer*rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

           

            [self.imageViewaddGestureRecognizer:rotation];

           

       }

            break;

       case 3://清扫

       {

            self.imageView.image = [UIImage imageNamed:@"1.jpg"];

            self.imageView.contentMode =UIViewContentModeScaleAspectFit;

            //初始值为1

            self.number = 1;

           

            UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

            //设置方向(只支持一个方向),因为swipe只支持一个方向, 所以要想实现两个方向的清扫, 需要添加两个swipe   清扫手势的滑动方向  上 下 左  右

          

            swipe.direction =UISwipeGestureRecognizerDirectionRight;

            [self.imageView addGestureRecognizer:swipe];

           

            UISwipeGestureRecognizer *swipe1 =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

           

            swipe1.direction =UISwipeGestureRecognizerDirectionLeft;

            [self.imageView addGestureRecognizer:swipe1];

           

           

       }

            break;

       case 4://平移

       {

            UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

            [self.imageView addGestureRecognizer:pan];

                    }

           

            break;

       case 5://边缘移动

       {

            UIScreenEdgePanGestureRecognizer*edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan:)];

            //边缘平移只支持一个方向

            edgePan.edges = UIRectEdgeLeft;

            [self.imageViewaddGestureRecognizer:edgePan];

          

       }

            break;

       case 6://长按

       {

            UILongPressGestureRecognizer*longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

            //配置长按手势 触发的时间

            longPress.minimumPressDuration = 3; //3s触发

            //允许点击位置变动的距离

            longPress.allowableMovement = 200;

            [self.imageViewaddGestureRecognizer:longPress];

                    }

            break;

       default:

            break;

   }

}

 

//点击手势方法

- (void)tap {

   self.number++;

   if (self.number == 4) {

       self.number = 1;

       //一共三张图片, 当number增加到4的时候, 令number等于1,跳转到第一张图片

   }

   NSString *name = [NSString stringWithFormat:@"%d.jpg", self.number];

   self.imageView.image = [UIImage imageNamed:name];

   

}

 

//捏合手势方法

- (void)pinch:(UIPinchGestureRecognizer*)aPinch {

   //缩放比率

   NSLog(@"%.2f", aPinch.scale);

   //transform是UIView的属性

   //图片的变形

   //参数1: 原先视图的transform

   //参数2: x轴上改变的比率

   //参数3: y轴上改变的比率

   self.imageView.transform = CGAffineTransformScale(self.imageView.transform, aPinch.scale,aPinch.scale);

   

   //重置比率

   aPinch.scale = 1;

}

 

//旋转手势方法

- (void)rotation:(UIRotationGestureRecognizer*)aRotation {

   //旋转角度

   NSLog(@"%.2f", aRotation.rotation);

   //改变图片旋转角度

   //参数1. 视图原先的transform

   //参数2, 变化的角度

   self.imageView.transform = CGAffineTransformRotate(self.imageView.transform,aRotation.rotation);

   //重置角度

   aRotation.rotation = 0;

}

 

//清扫手势方法

- (void)swipe:(UISwipeGestureRecognizer*)aSwipe {

   //假设两种情况, 当清扫方向为左的时候, 则增加number,为右的时候, 则减少number,从而实现左右清扫图片的效果

   if (aSwipe.direction ==UISwipeGestureRecognizerDirectionLeft) {

       self.number++;

       if (self.number == 4) {

            self.number = 1;

       }

   }

   if (aSwipe.direction ==UISwipeGestureRecognizerDirectionRight) {

       self.number--;

       if (self.number == 0) {

            self.number = 3;

       }

   }

   NSString *name = [NSString stringWithFormat:@"%d.jpg", self.number];

   self.imageView.image = [UIImage imageNamed:name];

   

}

 

//平移手势方法

- (void)pan:(UIPanGestureRecognizer *)aPan {

   //手指移动的位置(x轴和y轴偏移量)

   CGPoint point = [aPan translationInView:self.imageView];

   NSLog(@"%@", NSStringFromCGPoint(point));

   

   self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);

   //重置point

   [aPan setTranslation:CGPointZero inView:self.imageView];

   

}

 

//边缘移动手势方法

- (void)edgePan:(UIScreenEdgePanGestureRecognizer*)aEdgePan {

   NSLog(@"%s", __FUNCTION__);

}

 

//长按手势方法

- (void)longPress:(UILongPressGestureRecognizer*)aLongPress {

   NSLog(@"%s", __FUNCTION__);

   //手势状态

   //aLongPress.state

   //手势状态共有七种

   //UIGestureRecognizerStateBegan,UIGestureRecognizerStatePossible, UIGestureRecognizerStateChanged,UIGestureRecognizerStateEnded, UIGestureRecognizerStateCancelled, UIGestureRecognizerStateFailed,UIGestureRecognizerStateRecognized

   if (aLongPress.state == UIGestureRecognizerStateBegan) {

       NSLog(@"开始");

       //长按实现屏幕截图

       //屏幕截图

       UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);

       [self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];

       UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

       UIGraphicsEndImageContext();

       UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);

   }

}

 

//为屏幕截图添加一个提示

- (void) image:(UIImage *) imagedidFinishSavingWithError:(NSError *) error contextInfo:(void *) contextInfo {

   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片已保存!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

   [alertView show];

}

 

 

UISlider

1.UISlider
    //UISlider是iOS中的滑块控件
    //它一般用来控制视频的播放进度、音量等等
    //继承于UIControl,滑块提供了⼀一系列连续的值,滑块停在不同的位置,获取到滑块上的值也不同
   
    //UISlider的属性
    minimumValue //设置滑块的最⼩值
    maximumValue //设置滑块的最大值
    value //设置滑块的当前值
    minimumTrackTinkColor //定义划过区域的颜⾊
   addTarget: action: forControlEvents: 给UISlider添加事件, controlEvent为UIControlEventValueChanged

    //滑动器的大小固定, 高度影响触摸范围, 如果为0, 不能进行交互
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(50, 300, 280, 40)];
    slider.tag = 101;
    slider.minimumValue = 10;
    slider.maximumValue = 20;
    [slider setValue:0.5 animated:YES];
    [slider addTarget:self action:@selector(slide:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    [slider release];

- (void)slide:(UISlider *)aSlider {
    //value, 默认取值范围[0, 1]
    NSLog(@"%.2f", aSlider.value);
}

//2.创建动画数组
    //UIImageView
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
    imageView.center = self.view.center;
   
    //创建动画数组
    NSMutableArray *imageArray = [[NSMutableArray alloc]initWithCapacity:0];
    for (NSInteger i = 1; i <= 14; i++) {
        NSString *name = [NSStringstringWithFormat:@"%ld", i];
        UIImage *tempImage = [UIImageimageNamed:name];
        [imageArray addObject:tempImage];
    }
    //设置imageView动画数组
    imageView.animationImages = imageArray;
    //设置动画时长, 默认一秒播放三十张
    imageView.animationDuration = 1.5;
    //动画重复次数, 默认0, 代表无限次
//    imageView.animationRepeatCount = 1;
    [self.view addSubview:imageView];
    [imageView release];
   
    //开始动画
    [imageView startAnimating];

//3.Delegate
//(1)制定协议, 协议名字格式: 类名 + Delegate
//在.h中写

@protocol TouchViewDelegate

@optional
- (void)touchBegin:(TouchView *)touchView;

- (void)touchMove:(TouchView *)touchView;

- (void)touchEnd:(TouchView *)touchView;

- (void)touchCancel:(TouchView *)touchView;

@end

@interface TouchView : UIView

//(2)写属性, 属性名delegate, 类型是id, 并且要遵守协议<类名Delegate>
//在.h中写
@property (nonatomic, assign) id delegate;

@end

//(3)一旦找到代理, 让代理执行事情
//在.m中写
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //判断delegate是否实现了某个方法
    if ([_delegate respondsToSelector:@selector(touchBegin:)]) {
        [_delegate touchBegin:self];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([_delegate respondsToSelector:@selector(touchMove:)]) {
        [_delegate touchMove:self];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([_delegate respondsToSelector:@selector(touchEnd:)]) {
        [_delegate touchEnd:self];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([_delegate respondsToSelector:@selector(touchCancel:)]){
        [_delegate touchCancel:self];
    }
}

@end

 

1.UIScrollView
    //滚动视图类, 继承于UIView
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //frame决定可视范围, contenstSize决定显示内容页大小
    //如果contenstSize小于frame, 内容不足一页, screenView不能滚动
    //contenstSize大于frame, screenView才能滚动
   
    //设置内容大小, 内容宽度超过frame.width, 支持水平滚动; 内容高度超过frame.height, 支持垂直滚动
    scrollView.contentSize = CGSizeMake([UIScreenmainScreen].bounds.size.width + 500, 1000);
    //能否滚动
    scrollView.scrollEnabled = YES;
    //是否显示水平滚动条
    scrollView.showsHorizontalScrollIndicator = YES;//横
    //是否显示垂直滚动条
    scrollView.showsVerticalScrollIndicator = YES;//竖

    //YES, 如果开始时是 水平或垂直,只支持一个方向滚动; 如果开始时是 对角线方向, 支持水平和垂直同时滚动
    scrollView.directionalLockEnabled = YES;
   
    //取消回弹
    //scrollView.bounces = NO;//默认回弹
    //当contentSize.height < frame.height, 垂直方向是否支持回弹
    scrollView.alwaysBounceVertical = YES;
    //当contentSize.width < frame.weight, 水平方向是否支持回弹
    scrollView.alwaysBounceHorizontal = YES;
   
    //点击状态栏回到顶部
    scrollView.scrollsToTop = YES;
   
    //内容偏移量
    scrollView.contentOffset = CGPointMake(200, 0);
   
    //注: 缩放必须配合代理才能够使用
    //最小缩放比例
    scrollView.minimumZoomScale = 0.5;
    //最大缩放比例
    scrollView.maximumZoomScale = 2;
    //缩放比例
    scrollView.zoomScale = 1.0;
   
    //设置delegate
    scrollView.delegate = self;

    //滚动到顶部(添加按钮实现)
    scrollView.contentOffset = CGPointZero;
    [scrollView setContentOffset:CGPointZero animated:YES];
   
    //滚动到某个区域(添加按钮实现)
    [scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1)animated:YES];
   
    //滚动到底部(添加按钮实现)
    CGFloat height = scrollView.contentSize.height -scrollView.frame.size.height;
    [scrollView setContentOffset:CGPointMake(0, height)animated:YES];
   
    //设置背景颜色
    scrollView.backgroundColor = [UIColor greenColor];
    //添加父视图
    [self.view addSubview:scrollView];
    //释放
    [scrollView release];

2.UIPageControl
    //页码控制器, 继承于UIControl
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 400, 375, 40)];
    pageControl.backgroundColor = [UIColor grayColor];
   
    //设置页数
    pageControl.numberOfPages = 10;
    //设置当前页, 从0开始
    pageControl.currentPage = 0;
    //如果只有一页, 是否隐藏
    pageControl.hidesForSinglePage = YES;
    //圆点的颜色
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    //当前圆点的颜色
    pageControl.currentPageIndicatorTintColor = [UIColorgreenColor];
   
    [self.view addSubview:pageControl];
    [pageControl release];

0 0
原创粉丝点击