事件处理和手势及转场动画

来源:互联网 发布:期货研究员 知乎 编辑:程序博客网 时间:2024/06/06 03:39

【视图与UITouch对应的方法】

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

{

    //获取点击事件

    UITouch *t = [touches anyObject];

    //如果点击的是图片就把他移到视图的最上层

    if ([t.viewisKindOfClass:[UIImageViewclass]]) {

        [self.viewbringSubviewToFront:t.view];

    }

}

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

{

    UITouch *t = [touches anyObject];

    //如果点击的是图片就让他移动

    if ([t.viewisKindOfClass:[UIImageViewclass]]) {

        CGPoint p = [t locationInView:self.view];

        t.view.center = p;

    }

}

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

{

    UITouch *t = [touches anyObject];

    //点到第二次的时候,如果点击的是图片就让图片变长

    if ([t.viewisKindOfClass:[UIImageViewclass]]) {

        if (t.tapCount ==2) {

            CGRect rect = t.view.frame;

            rect.size.height +=10;

            t.view.frame = rect;

        }

    }

}



UIGestureRecognizer】手势识别器

6种手势

UITapGestureRecognizer  Tap(点击)

UIPanGestureRecognizer Pan (拖移,移动)
UIPinchGestureRecognizer Pinch(缩放)
UIRotationGestureRecognizer Rotation(旋转)
UILongPressGestureRecognizer    LongPress(长按)
UISwipeGestureRecognizer Swipe(轻扫)


- (void)tapGR:(UITapGestureRecognizer *)tapGR

{

    NSLog(@"点一下");

    [self.viewbringSubviewToFront:tapGR.view];

}


- (void)panGR:(UIPanGestureRecognizer *)panGR

{

    NSLog(@"拖动");

    CGPoint translation = [panGR translationInView:self.view];

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

    [panGR setTranslation:CGPointZeroinView:self.view];

}


- (void)pinchGR:(UIPinchGestureRecognizer *)pinchGr

{

    NSLog(@"缩放");

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

    pinchGr.scale = 1;

}


- (void)rotationGR:(UIRotationGestureRecognizer *)rotationGR

{

    NSLog(@"旋转");

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

    rotationGR.rotation = 0;

}


- (void)lpGR:(UILongPressGestureRecognizer *)lpGR

{

    NSLog(@"长按");

    if (lpGR.state ==UIGestureRecognizerStateEnded) {


        CABasicAnimation  *shake=[CABasicAnimationanimationWithKeyPath:@"transform.scale"];


//transform.scale代表缩放(倍数)

//transform.rotation.x  transform.rotation.y  transform.rotation.z 代表旋转(角度)


 //动画的初始值和结束值

        shake.fromValue=[NSNumbernumberWithFloat:0.5];

        shake.toValue=[NSNumbernumberWithFloat:1.5];


        //单次时间

        shake.duration=0.2;

        //重复次数

        shake.repeatCount=15;

        //动画返回

        shake.autoreverses=YES;


//添加动画

        [lpGR.view.layeraddAnimation:shake forKey:@"imageShake"];


//取消动画

//[lpgr.view.layer removeAnimationForKey:@"imageShake"];

    }

}


-(void)swipe:(UISwipeGestureRecognizer*)swipeGesture

{

    NSLog(@"轻扫,与拖动互斥");

    if (swipeGesture.direction ==UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"dowm");

    } elseif (swipeGesture.direction ==UISwipeGestureRecognizerDirectionUp) {

        NSLog(@"up");

    } elseif (swipeGesture.direction ==UISwipeGestureRecognizerDirectionLeft) {

        NSLog(@"left");

    } else {

        NSLog(@"right");

    }

}



转场动画


XXXViewController *xvc = [[XXXViewControlleralloc]init];


CATransition *ani = [CATransitionanimation];

ani.duration =0.5f;


//方向

ani.subtype =kCATransitionFromTop;

//四种预设,某些类型中此设置无效

kCATransitionFromRight

kCATransitionFromLeft

kCATransitionFromTop

kCATransitionFromBottom


//类型

ani.type =kCATransitionMoveIn;


1.#define定义的常量 (基本型)

     kCATransitionFade   交叉淡化过渡 

kCATransitionMoveIn 新视图移到旧视图上面 

     kCATransitionPush   新视图把旧视图推出去 

     kCATransitionReveal 将旧视图移开,显示下面的新视图 


2.用字符串表示

fademoveIn push reveal 和上面的四种一样

pageCurl pageUnCurl翻页

rippleEffect 滴水效果 

suckEffect 收缩效果,如一块布被抽走 

cube alignedCube 立方体效果 

flip alignedFlip oglFlip翻转效果  

rotate旋转

cameraIriscameraIrisHollowOpen cameraIrisHollowClose 相机



    [self.navigationController.view.layeraddAnimation:aniforKey:@"animation"];

    [self.navigationControllerpushViewController:fvc animated:NO];





- (UIImage *)clipImage:(UIImage *)image inRect:(CGRect)rect

{//返回imagerect范围内的图片

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

    UIImage *subImage = [UIImageimageWithCGImage:imageRef];

    return subImage;

}

0 0
原创粉丝点击