仿微信截取视频选择框

//// ViewController.m// ceShi//// Created by CFS on 2017/10/23.// Copyright © 2017年 CFS. All rights reserved.//#import "ViewController.h"#import "UIView+Frame.h"@interface ViewController ()//中间view@property (strong, nonatomic) IBOutlet UIView *center;//左view@property (strong, nonatomic) IBOutlet UIView *left;//右view@property (strong, nonatomic) IBOutlet UIView *right;//是否可以向左移动标记@property (nonatomic, assign) BOOL canLeft;//是否可以向右移动标记@property (nonatomic, assign) BOOL canRight;//最开始最左侧记录点位@property (nonatomic, assign) CGFloat MAXLeft;//最开始最右侧记录@property (nonatomic, assign) CGFloat MAXRight;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //初始化最开始最左最右 self.MAXLeft = self.left.x; self.MAXRight = self.right.maxX;}- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{ //如果标记左侧可动或者右侧可动还处罚此方法只能说明误触其他位置,马上强制取消移动 if (self.canLeft || self.canRight) { self.canLeft = NO; self.canRight = NO; return; } //手指触摸点的位置(以self.view为坐标系) CGPoint point = [[touches anyObject] locationInView:self.view]; //convert point to the white layer's coordinates //手指触摸点的位置(以self.left为坐标系) CGPoint point1 = [self.left.layer convertPoint:point fromLayer:self.view.layer]; //手指触摸点的位置(以self.right为坐标系) CGPoint point2 = [self.right.layer convertPoint:point fromLayer:self.view.layer]; //get layer using containsPoint: //判断点在左边view还是在右边view 手按下时候这个点在哪个view就允许哪个view移动 if ([self.left.layer containsPoint:point1]) { self.canLeft = YES; }else if ([self.right.layer containsPoint:point2]){ self.canRight = YES; }}- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event{ //手指当前的点位 CGPoint point = [[touches anyObject] locationInView:self.view]; //如果手指在初始范围外 则不移动 if (point.x < self.MAXLeft || point.x > self.MAXRight) { return; } //如果左侧可以移动 if (self.canLeft) { //如果左侧要超过右侧了 不动 if (point.x + self.left.width > self.right.x - 20) { point.x = self.right.x - self.left.width - 20; } //左侧view的x跟随手指 self.left.x = point.x; //中间view的x跟随左侧view self.center.x = self.left.maxX; //中间view的宽由左右view的间距决定 self.center.width = self.right.x - self.left.maxX; }else if(self.canRight){ //如果右侧超过左侧了 不动 if (point.x < self.left.maxX + 20) { point.x = self.left.maxX + 20; } //右侧veiw的x跟随手指 self.right.x = point.x; //中心view的宽由左右view的间距决定 由于动右侧中间view的x肯定不动,所以无需计算 self.center.width = self.right.x - self.left.maxX; }}- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{

//当手指离开屏幕,不允许移动,等待下次点击到左右view

self.canLeft = NO;

self.canRight = NO;

NSLog(@"zuo%0.2f  you%0.2f",self.left.x,self.right.maxX);

}

@end

你可能感兴趣的:(仿微信截取视频选择框)