iOS:同时调整大小并旋转UIView

时间:2015-09-14 17:18:57

标签: ios objective-c uiview

在我的视图控制器中使用UIPanGestureRecognizer,我尝试根据触摸位置以一定角度绘制视图(ArrowView)。我尝试使用CGAffineTransformRotate根据第一次触摸和当前触摸之间的角度旋转视图,但这不起作用,除非视图已经被绘制了至少20个或更多像素。此外,在绘制时,视图并不总是在我的手指下排成一行。对于这种情况,这是正确的方法吗?如果没有,是否有人建议更好的方法来实现这一目标?如果是这样,我做错了什么?

ViewController.m

@implementation ViewController {
    ArrowView *_selectedArrowView;
    UIColor *_selectedColor;
    CGFloat _selectedWeight;
    CGPoint _startPoint;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _selectedColor = [UIColor yellowColor];
    _selectedWeight = 3;

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
    [self.view addGestureRecognizer:pan];
}

- (void) panHandler: (UIPanGestureRecognizer *) sender {
    if (sender.state == UIGestureRecognizerStateBegan) {

        //Instantiate the arrow
        CGPoint touchPoint = [sender locationInView:sender.view];
        _startPoint = touchPoint;
        _selectedArrowView = [[ArrowView alloc] initWithFrame:CGRectMake(touchPoint.x, touchPoint.y, 0, 25) withColor:_selectedColor withWeight:_selectedWeight];
        _selectedArrowView.delegate = self;
        [self.view addSubview:_selectedArrowView];
        [self.view bringSubviewToFront:_selectedArrowView];

    } else if (sender.state == UIGestureRecognizerStateChanged) {

        //"Draw" the arrow based upon finger postion
        CGPoint touchPoint = [sender locationInView:sender.view];
        [_selectedArrowView drawArrow:_startPoint to:touchPoint];
    }
}

@end

ArrowView.m

- (void) drawArrow: (CGPoint) startPoint to: (CGPoint) endPoint {
    startPoint = [self convertPoint:startPoint fromView:self.superview];
    endPoint = [self convertPoint:endPoint fromView:self.superview];

    if (_initialAngle == -1000 /*Initially set to an arbitrary value so I know when the draw began*/) {
        _initialAngle = atan2(startPoint.y - endPoint.y, startPoint.x - endPoint.x);
        [self setPosition:0];
    } else {
        CGFloat ang = atan2(startPoint.y - endPoint.y, startPoint.x - endPoint.x);
        ang -= _initialAngle;
        self.transform = CGAffineTransformRotate(self.transform, ang);

        CGFloat diff = (endPoint.x - self.bounds.size.width);

        NSLog(@"\n\n diff: %f \n\n", diff);
        self.bounds = CGRectMake(0, 0, self.bounds.size.width + diff, self.bounds.size.height);

        _endPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height);
        [self setNeedsDisplay];
    }
}

- (void) setPosition: (CGFloat) anchorPointX {
    CGPoint layerLoc;

    if (anchorPointX == 0) {
        layerLoc = CGPointMake(self.layer.bounds.origin.x, self.layer.bounds.origin.y + (self.layer.bounds.size.height / 2));
    } else {
        layerLoc = CGPointMake(self.layer.bounds.origin.x + self.layer.bounds.size.width, self.layer.bounds.origin.y + (self.layer.bounds.size.height / 2));
    }

    CGPoint superLoc = [self convertPoint:layerLoc toView:self.superview];

    self.layer.anchorPoint = CGPointMake(anchorPointX, 0.5);
    self.layer.position = superLoc;
}

- (CGFloat) pToA: (CGPoint) touchPoint {
    CGPoint start;
    if (_dotButtonIndex == kDotButtonFirst) {
        start = CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMaxY(self.bounds));
    } else {
        start = CGPointMake(CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds));
    }
    return atan2(start.y - touchPoint.y, start.x - touchPoint.x);
}

链接到GitHub上的项目:Project Link

1 个答案:

答案 0 :(得分:0)

想出来。

我必须让它有一个初始宽度,这样角度才能起作用。

_initialAngle = atan2(startPoint.y - endPoint.y, startPoint.x - (endPoint.x + self.frame.size.width));