UITextView旋转和缩放IOS

时间:2016-07-01 13:03:00

标签: ios objective-c rotation uitextview

如何通过捏合手势使UITextView可调整大小以及缩放(调整大小)它的能力。

问题是在使用旋转调整大小时会变得模糊。

以下是我的代码:

- (void)handlePinchOrRotateGesture:(UIGestureRecognizer *)recognizer
{
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan: {
            if ([recognizer isKindOfClass:[UIRotationGestureRecognizer class]]) {
                self.currentRotateTransform = self.referenceRotateTransform;
                self.activeRotationRecognizer = (UIRotationGestureRecognizer *)recognizer;
            } else {
             NSLog(@"height %f, width %f",currentTF.frame.size.height,currentTF.frame.size.width);
                self.activePinchRecognizer = (UIPinchGestureRecognizer *)recognizer;
            }
            break;
        }

        case UIGestureRecognizerStateChanged: {

            CGAffineTransform currentTransform = self.referenceRotateTransform;

            if ([recognizer isKindOfClass:[UIRotationGestureRecognizer class]]) {
                self.currentRotateTransform = [self.class applyRecognizer:recognizer toTransform:self.referenceRotateTransform];
            }

            currentTransform = [self.class applyRecognizer:self.activePinchRecognizer toTransform:currentTransform];
           currentTransform = [self.class applyRecognizer:self.activeRotationRecognizer toTransform:currentTransform];

            currentTF.transform = currentTransform;

            break;
        }

        case UIGestureRecognizerStateEnded:
        {
            CGAffineTransform currentTransform = self.referenceRotateTransform;
            if ([recognizer isKindOfClass:[UIRotationGestureRecognizer class]]) {

                self.referenceRotateTransform = [self.class applyRecognizer:recognizer toTransform:self.referenceRotateTransform];
                self.currentRotateTransform = self.referenceRotateTransform;
                self.activeRotationRecognizer = nil;

            } else if ([recognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {

                self.scale *= [(UIPinchGestureRecognizer *)recognizer scale];
                self.activePinchRecognizer = nil;
                CGFloat currentFontSize = fontSize * _scale;
                if (!isRotaed)
                {

                if (CGPointEqualToPoint(self.referenceCenter, CGPointZero)) {
                    currentTF.center = CGPointMake(CGRectGetMidX(self.view.bounds),
                                                        CGRectGetMidY(self.view.bounds));
                }
                 currentTransform = [self.class applyRecognizer:self.activePinchRecognizer toTransform:currentTransform];
                 NSLog(@"height %f, width %f",currentTF.frame.size.height,currentTF.frame.size.width);
                currentTF.transform = currentTransform;

                    UITextView *newText = [[UITextView alloc]init];
                    newText.scrollEnabled=YES;
                    newText.text=currentTF.text;
                    newText.font =[UIFont fontWithName:strFont size:currentFontSize];
                    [newText sizeToFit];
                    newText.scrollEnabled=NO;

                    currentTF.font = [UIFont fontWithName:strFont size:currentFontSize];
                    NSLog(@"height %f, width %f",currentTF.frame.size.height,currentTF.frame.size.width);
                    currentTF.frame =CGRectIntegral(newText.frame);
                    currentTF.center = CGPointMake(CGRectGetMidX(self.veText.bounds),
                                                   CGRectGetMidY(self.veText.bounds));

                }

            }

            break;
        }

        default:
            break;
    }
}

+ (CGAffineTransform)applyRecognizer:(UIGestureRecognizer *)recognizer toTransform:(CGAffineTransform)transform
{
    if (!recognizer
        || !([recognizer isKindOfClass:[UIRotationGestureRecognizer class]]
             || [recognizer isKindOfClass:[UIPinchGestureRecognizer class]])) {
            return transform;
        }

    if ([recognizer isKindOfClass:[UIRotationGestureRecognizer class]]) {

        return CGAffineTransformRotate(transform, [(UIRotationGestureRecognizer *)recognizer rotation]);
    }

    CGFloat scale = [(UIPinchGestureRecognizer *)recognizer scale];
    return CGAffineTransformScale(transform, scale, scale);
}

0 个答案:

没有答案