UI第八天

来源:互联网 发布:建筑工程网络计划图 编辑:程序博客网 时间:2024/06/05 16:02

注意:

只有加了下面的代码后,对同一个View才能同时使用两种手势:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}


一些概念:

1.//什么是响应者对象?

    //只要是能够接收事件并且能响应事件的对象,我们称作为响应者对象!在iOSUIResponder这个类所创建的对象都是响应者对象!

    

    //UIApplication是用来管理事件的,管理事件的方式是队列的方式(FIFO

    //前提条件都是响应者对象  用户交互处于打开状态

    //事件的分发顺序:UIApplication--->window--->viewController--->self.view--->self.view的子视图

    //事件的响应顺序:self.view的子视图--->self.view--->viewController--->window--->UIApplication--->废弃

    

    //事件没有响应的几个原因:

    //用户交互处于关闭状态

    //父视图的用户交互是否打开

    //子视图超出父视图

    //window的大小为全屏大小(window是产生事件的)


2.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"ViewController");

}


3.  //单击手势

    UITapGestureRecognizer *tapOne = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap:)];

    //设置点击次数默认为1

    tapOne.numberOfTapsRequired =1;

    //设置手指个数默认为1

    tapOne.numberOfTouchesRequired =1;

    

    //加手势

    [imageViewaddGestureRecognizer:tapOne];


- (void)tap:(UITapGestureRecognizer *)tap

{

    //相对于某一个视图获取手势点击的位置

//    [tap locationInView:tap.view]

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

    

    //tap.view 通过这个属性我们可以获得添加此手势的视图

    NSLog(@"图片被点击---%@---%@", tap.view, NSStringFromCGPoint(point));

}


4//单指双击

    UITapGestureRecognizer *tapTwo = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapTwo:)];

    

    tapTwo.numberOfTapsRequired =2;

    

    [imageViewaddGestureRecognizer:tapTwo];

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

    [tapOne requireGestureRecognizerToFail:tapTwo];


- (void)tapTwo:(UITapGestureRecognizer *)tap

{

   NSLog(@"双击");

   if (isTap ==NO)

    {

        [UIViewanimateWithDuration:0.5animations:^{

            tap.view.frame = [UIScreenmainScreen].bounds;


        }];

    }

   else

    {

        [UIViewanimateWithDuration:0.5animations:^{

             tap.view.frame =CGRectMake(40,100, 200, 200);

        }];

    }

    isTap = !isTap;

}


5.  //长按手势长按手势我们需要注意手势的状态  state

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];

    //设置长按多少秒之后开始执行方法  默认为0.5

    longPress.minimumPressDuration =1.0;

    

    [imageViewaddGestureRecognizer:longPress];


- (void)longPress:(UILongPressGestureRecognizer *)longPress

{

    if (longPress.state ==UIGestureRecognizerStateBegan)

    {

        NSLog(@"长按手势");

    }

    elseif (longPress.state ==UIGestureRecognizerStateEnded)

    {

       NSLog(@"长按手势结束");

    }

}


6. //滑动手势

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe:)];

    

    swipe.direction =UISwipeGestureRecognizerDirectionLeft;


    [imageViewaddGestureRecognizer:swipe];

    

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe:)];

    

    swipeRight.direction =UISwipeGestureRecognizerDirectionRight;

    

    [imageViewaddGestureRecognizer:swipeRight];


- (void)swipe:(UISwipeGestureRecognizer *)swipe

{

    if (swipe.direction ==UISwipeGestureRecognizerDirectionLeft)

    {

        SecondViewController *vc2 = [[SecondViewControlleralloc]init];

        

        [self.navigationControllerpushViewController:vc2 animated:YES];

    }

    elseif (swipe.direction ==UISwipeGestureRecognizerDirectionRight)

    {

        SecondViewController *vc2 = [[SecondViewControlleralloc]init];

        

        [self.navigationControllerpushViewController:vc2 animated:YES];

    }

}


7.  //移动手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];

    

    [view addGestureRecognizer:pan];


- (void)pan:(UIPanGestureRecognizer *)pan

{

   UIView *view = pan.view;

    //法一:

    //获得偏移量

//    CGPoint point = [pan translationInView:self.view];

//    view.frame = CGRectMake(point.x+view.frame.origin.x, point.y+view.frame.origin.y, view.frame.size.width, view.frame.size.height);

////    CGPointZero 相当于 CGPointMake(0, 0)

//    [pan setTranslation:CGPointZero inView:self.view];

    

    //法二:

   CGPoint point = [pan translationInView:self.view];

//    view.transform = CGAffineTransformTranslate(view.transform, point.x, point.y);

    //上面的self.view改为view旋转后再移动就不变

    view.center =CGPointMake(point.x+view.center.x, point.y+view.center.y);

    [pan setTranslation:CGPointZeroinView:self.view];

}


8.//捏合手势

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)];

    pinch.delegate =self;

    [view addGestureRecognizer:pinch];


- (void)pinch:(UIPinchGestureRecognizer *)pinch

{

   UIView *view = pinch.view;

   CGFloat scale = pinch.scale;

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

    pinch.scale =1.0;

}


9.//旋转手势

    UIRotationGestureRecognizer *rotatation = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotate:)];

    [viewaddGestureRecognizer:rotatation];


- (void)rotate:(UIRotationGestureRecognizer *)rotate

{

   UIView *view = rotate.view;

   CGFloat rotation = rotate.rotation;

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

    rotate.rotation =0.0;

}


0 0