触摸与手势中要求拖拽出一个半透明黑框,并打印位置和大小
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
效果图见下:
}