美文网首页
仿苹果系统相机缩放控件封装

仿苹果系统相机缩放控件封装

作者: 电动鸡翅 | 来源:发表于2019-09-28 13:44 被阅读0次

no图说个jb。先上图:


zoom_pre.gif

demo路径 https://github.com/DawnzrXie/AppleZoomView

1.圆心不动先创建好控件(背景大圆,小点所在的圆)

- (void)configurationUI {
    
    //加转动手势
    UIPanGestureRecognizer *pgr =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(zhuanPgr:)];
    [self addGestureRecognizer:pgr];
    
    self.layer.masksToBounds = YES;
    self.bigRoundView = [[UIView alloc] init];
    self.bigRoundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    [self addSubview:self.bigRoundView];
    
    self.smallRoundBGView = [[UIView alloc] init];
    [self addSubview:self.smallRoundBGView];
    
    [self.bigRoundView addSubview:self.multipleLabel];
    
    for (int i=0; i<self.number; i++) {
        
        if (i==0 || i==27) {
            UILabel *zoomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
            zoomLabel.textColor = [UIColor whiteColor];
            zoomLabel.font = [UIFont systemFontOfSize:13];
            if (i == 0) {
                zoomLabel.textAlignment = NSTextAlignmentLeft;
                zoomLabel.text = @"1x";
            } else {
                zoomLabel.textAlignment = NSTextAlignmentRight;
                zoomLabel.text = @"7x";
            }
            [self.smallRoundBGView addSubview:zoomLabel];
            [self.subViewArray addObject:zoomLabel];
        } else {
            UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 4)];
            subView.backgroundColor = [UIColor whiteColor];
            subView.layer.cornerRadius = 2;
            subView.layer.masksToBounds = YES;
            [self.smallRoundBGView addSubview:subView];
            [self.subViewArray addObject:subView];
        }
        
        
    }
    
    [self refreshUI];
}

2.刷新UI

#pragma mark -- 刷新UI
- (void)refreshUI
{
    
    //大圆
    self.bigRoundView.frame = CGRectMake(0, 0, 2*self.radius, 2*self.radius);
    self.bigRoundView.center = self.roundCenterPoint;
    self.bigRoundView.layer.cornerRadius = self.radius;
    self.bigRoundView.layer.masksToBounds = YES;
    self.bigRoundView.layer.borderWidth = 1;
    self.bigRoundView.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.2].CGColor;
    
    //小圆点背景
    self.smallRoundBGView.frame = CGRectMake(0, 0, 2*self.radius, 2*self.radius);
    self.smallRoundBGView.center = self.roundCenterPoint;
    
    
    //显示倍数的label
    self.multipleLabel.center = CGPointMake(self.radius, self.margin);
    
    self.pointArray = [self creatPointArrayWithPoint:self.roundCenterPoint r:self.radius-self.margin];
    
    for (int i=0; i<self.pointArray.count; i++) {
        CGPoint newPoint = CGPointFromString(self.pointArray[i]);
        if (i==0 || i==27) {
            UILabel *zoomLabel = self.subViewArray[i];
            zoomLabel.center = newPoint;
        } else {
            UIView *subView = self.subViewArray[i];
            subView.center = newPoint;
        }
    }
    
}

3.出现动画和消失动画

//开始动画
- (void)startZoomScaleViewWithScaleNum:(NSInteger)scaleNum {
    self.startAngle = self.subViewAngle-(scaleNum-1)*self.subViewAngle/6;
    self.pointArray = [self creatPointArrayWithPoint:self.roundCenterPoint r:self.radius-self.margin];
    for (int i=0; i<self.pointArray.count; i++) {
        
        CGPoint newPoint = CGPointFromString(self.pointArray[i]);
        UIView *subView = self.subViewArray[i];
        subView.center = newPoint;
    }
    
    self.multipleLabel.text = [NSString stringWithFormat:@"%ldx",(long)scaleNum];
    
    
    //    type == AUTZOOM_TYPE_START
    self.userInteractionEnabled = NO;
    self.radius = 13*SCREEN_WIDTH/24 - SCREEN_WIDTH/3;
    [self addShowDrawTimer];
    self.hidden = NO;
}

//动画消失
- (void)refreshUIWithZoomDismiss {
    
    //        AUTZOOM_TYPE_END
    self.userInteractionEnabled = NO;
    self.radius = 13*SCREEN_WIDTH/24;
    [self addDismissDrawTimer];
    self.hidden = YES;
}

4.手势转动动画

#pragma mark - 转动手势
-(void)zhuanPgr:(UIPanGestureRecognizer *)pgr
{
    
    if(pgr.state==UIGestureRecognizerStateBegan){
        
        self.beginPoint=[pgr locationInView:self];
        self.isBeginTouch = YES;
        if (self.isPanBlock) {
            self.isPanBlock(YES);
        }
    }else if (pgr.state==UIGestureRecognizerStateChanged){
        self.movePoint= [pgr locationInView:self];
        
        [self scrollowRadius];
    }else if (pgr.state==UIGestureRecognizerStateEnded){
        
        if (self.isPanBlock) {
            self.isPanBlock(NO);
        }
        [self scrollowRadius];
    }
}

#pragma mark -- 移动小点
- (void)scrollowRadius {
    
    CGFloat beginAngle = 0;
    if (self.beginPoint.x <SCREEN_WIDTH/2) {
        beginAngle = atan((self.radius-self.beginPoint.y)/(SCREEN_WIDTH/2-self.beginPoint.x));
    } else {
        beginAngle = M_PI - atan((self.radius-self.beginPoint.y)/(self.beginPoint.x-SCREEN_WIDTH/2));
    }
    
    CGFloat moveAngle = 0;
    if (self.movePoint.x <SCREEN_WIDTH/2) {
        moveAngle = atan((self.radius-self.movePoint.y)/(SCREEN_WIDTH/2-self.movePoint.x));
    } else {
        moveAngle = M_PI - atan((self.radius-self.movePoint.y)/(self.movePoint.x-SCREEN_WIDTH/2));
    }
    CGFloat moveX = self.movePoint.x-self.beginPoint.x;
    
    
    CGFloat angle = moveAngle - beginAngle;
    
    if (!self.isBeginTouch) {
        self.startAngle -= self.temPanAngle;
    }
    self.startAngle += angle;
    self.temPanAngle = angle;
    self.isBeginTouch = NO;

    NSLog(@"beginAngle= %f moveAngle= %f moveX=%f",beginAngle,moveAngle,M_PI_2*moveX/SCREEN_WIDTH);
    
    if (self.startAngle<0) {
        self.startAngle = 0;
    }
    if (self.startAngle > M_PI_2) {
        self.startAngle = M_PI_2;
    }
    NSLog(@"self.startAngle = %f",self.startAngle);
    self.pointArray = [self creatPointArrayWithPoint:self.roundCenterPoint r:self.radius-self.margin];
    for (int i=0; i<self.pointArray.count; i++) {
        
        CGPoint newPoint = CGPointFromString(self.pointArray[i]);
        UIView *subView = self.subViewArray[i];
        subView.center = newPoint;
    }
    
    NSInteger zoom = (self.subViewAngle - self.startAngle)/(self.subViewAngle/6)+1;
    self.multipleLabel.text = [NSString stringWithFormat:@"%ldx",(long)zoom];
    
    if (self.currentZoom) {
        self.currentZoom(zoom);
    }
}

5.使用方法

CGFloat bottomView_h = ([ViewController safeAreaInset].top > 0 ? 100 : 80);
    __weak typeof(self) weakSelf = self;
    //缩放控件
    self.zoomView = [[DawnXZRzoomView alloc] initWithBottom:bottomView_h margin:30 number:28];
    [self.view addSubview:self.zoomView];
    self.zoomView.isPanBlock = ^(BOOL isPan) {
        if (isPan) { //在缩放视图上
            [NSObject cancelPreviousPerformRequestsWithTarget:weakSelf selector:@selector(zoomViewHidden) object:nil];
        } else {
            [weakSelf performSelector:@selector(zoomViewHidden) withObject:nil afterDelay:2];
            
        }
    };
    self.zoomView.currentZoom = ^(NSInteger currentScale) {
        [weakSelf scaleBtnStatus:currentScale];
    };

详情见demo https://github.com/DawnzrXie/AppleZoomView

相关文章

网友评论

      本文标题:仿苹果系统相机缩放控件封装

      本文链接:https://www.haomeiwen.com/subject/hnswhctx.html