触摸与手势之拖拽


触摸与手势中要求拖拽出一个半透明黑框,并打印位置和大小

1.源代码

2.效果图


//  ViewController.m

#import "ViewController.h"


#define kScreenWidth [UIScreen mainScreen].bounds.size.width

#define kScreenHeight [UIScreen mainScreen].bounds.size.height


@interface ViewController ()


@end


@implementation ViewController


{

   UIView *_maskView ;//创建一个拖拽视图   

}


- (void)viewDidLoad {

    [superviewDidLoad];

    

   UIImageView *imgView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0, kScreenWidth,kScreenHeight)];

    imgView.image = [UIImageimageNamed:@"feng.jpeg"];

    [self.viewaddSubview:imgView];

    

    //拖拽手势

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

    [self.viewaddGestureRecognizer:pan];

    

    _maskView = [[UIViewalloc] initWithFrame:CGRectZero];

    _maskView.backgroundColor = [UIColorblackColor];

   _maskView.alpha =.5;//设置透明度

    [self.viewaddSubview:_maskView];

    

}

- (void)panAction:(UIPanGestureRecognizer *)pan

{

   static CGPoint firstPoint;

    if (pan.state ==UIGestureRecognizerStateBegan)//开始拖拽

    {

        firstPoint = [panlocationInView:self.view];

       NSLog(@"firstPoint is %@",NSStringFromCGPoint(firstPoint));

    }elseif (pan.state ==UIGestureRecognizerStateChanged)//拖拽手势移动

    {

       CGPoint lastPoint = [pan locationInView:self.view];

        NSLog(@"lastPoint is %@",NSStringFromCGPoint(lastPoint));


       _maskView.frame =CGRectMake(firstPoint.x, firstPoint.y, (lastPoint.x -firstPoint.x) , (lastPoint.y - firstPoint.y));

        NSLog(@"_maskView.frame is %@",NSStringFromCGRect(_maskView.frame));

    }

    elseif (pan.state ==UIGestureRecognizerStateEnded)//停止拖拽

    {

        _maskView.frame =CGRectZero;

    }

@end


效果图见下:

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值