Change uiview size after rotation transform

白昼怎懂夜的黑 提交于 2020-01-24 19:20:58

问题


My code needs some help from professional xcoders :-)

I have a draggable uitextview called "headline" that is a subview in "mainstage". I also added a pinch gesture to change the fontsize inside the uitextview. Everything works fine but the last essential feature that I really need is the rotation of the uitextview.

So I added a uislider for rotation and volia! it also works. But after this transformation the uitextview goes crazy* when it becomes a new size via the pinch gesture. (*The position and size of the uitextview)

After some research in the documentation I found „some useful warning“ (frame):

If you want the drawRect: method invoked when the frame rectangle changes, set the contentMode property to UIViewContentModeRedraw

Nice... but I don't know where I have to place it in my code...

In other words: How to change the frame size of a uiview after rotation? (myview.transform)

Thanks for everyone helps on this…

Here is my code:

- (void)viewDidLoad {

    // CREATE MAINSTAGE
    mainstage = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
    mainstage.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    mainstage.showsVerticalScrollIndicator=NO;
    mainstage.scrollEnabled=NO;
    mainstage.userInteractionEnabled=YES;
    mainstage.backgroundColor = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0];
    mainstage.layer.masksToBounds = YES;
    [self.view addSubview:mainstage];

    // CREATE HEADLINE
    headline = [[UITextView alloc]initWithFrame:CGRectMake(100, 100, 768, 1024)];
    headline.text = @"Headline\nNo1";
    headline.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    headline.textAlignment = NSTextAlignmentLeft;
    [headline setFont:[UIFont fontWithName:@"Helvetica" size:30]];
    [headline setScrollEnabled:NO];
    [headline setEditable:NO];
    [headline setUserInteractionEnabled:YES];
    [headline setBackgroundColor:[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.5]];
    [headline sizeToFit]; // RESET
    [mainstage addSubview:headline];

    // CREATE ROTATE SLIDER
    CGRect sliderFrameTextRotating = CGRectMake(0, 924, 768, 50);
    self.rotateSlider = [[UISlider alloc] initWithFrame:sliderFrameTextRotating];
    self.rotateSlider.minimumValue = -100.0f;
    self.rotateSlider.maximumValue = 100.0f;
    self.rotateSlider.value = 0.0f;
    [self.rotateSlider setContinuous:true];
    [self.rotateSlider addTarget:self action:@selector(getSliderRotatingValue:)forControlEvents:UIControlEventValueChanged];
    [self.rotateSlider addTarget:self action:@selector(RotateSliderDidEndSliding:)forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
    [mainstage addSubview:self.rotateSlider];


    // GESTURES

    /* drag */
    UIPanGestureRecognizer *TextdragRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(textviewDragged:)];
    [headline addGestureRecognizer:TextdragRecognizer];

    /* pinch */
    UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)];
    twoFingerPinch.cancelsTouchesInView = FALSE;
    twoFingerPinch.delaysTouchesEnded = TRUE;
    [headline addGestureRecognizer:twoFingerPinch];

    [super viewDidLoad];
}


// ROTATE
- (IBAction)getSliderRotatingValue:(id)paramSender {

    headline.transform = CGAffineTransformMakeRotation(rotateSlider.value * 2*M_PI / rotateSlider.maximumValue);
    headline.contentMode = UIViewContentModeRedraw; // ?
}

// ROTATE SLIDER END
- (void)RotateSliderDidEndSliding:(NSNotification *)notification {

}


// DRAG
- (void)textviewDragged:(UIPanGestureRecognizer *)gestureRecognizer {

    UIView *piece = (UIView *)gestureRecognizer.view;

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
    }

    else if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {

    }
}



// PINCH TEXT
- (void) twoFingerPinch:(UIPinchGestureRecognizer *) recognizer {

    CGPoint touchPoint = [recognizer locationInView:mainstage];
    UIView *touchView = [mainstage hitTest:touchPoint withEvent:nil];

    // BEGAN
    if (recognizer.state == UIGestureRecognizerStatePossible || recognizer.state == UIGestureRecognizerStateBegan) {

        if([touchView isKindOfClass:[UITextView class]]) {

            touchView.tag = 1;
            UITextView *myViewWithTag = (UITextView *)[touchView viewWithTag:1];
            myViewWithTag.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.5];

        }

    }

    // CHANGED
    else if (recognizer.state == UIGestureRecognizerStateChanged) {

        UITextView *myViewWithTag = (UITextView *)[self.view viewWithTag:1];
        touchView.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.5];

        CGFloat scale = recognizer.scale;
        myViewWithTag.font = [UIFont fontWithName:myViewWithTag.font.fontName size:myViewWithTag.font.pointSize*scale];
        [self textViewDidChange:myViewWithTag];

        recognizer.scale = 1.0f;

    }

    // ENDED
    else if (recognizer.state == UIGestureRecognizerStateEnded) {

        UITextView *myViewWithTag = (UITextView *)[self.view viewWithTag:1];
        touchView.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.5];

        if (myViewWithTag.textAlignment == NSTextAlignmentLeft) {

            [touchView sizeToFit];

        }
    }
}


// TEXTVIEW DID CHANGE
- (void)textViewDidChange:(UITextView *)textView {

    UITextView *myViewWithTag = (UITextView *)[textView viewWithTag:1];

    if (myViewWithTag.textAlignment == NSTextAlignmentLeft) {

        myViewWithTag.frame = CGRectMake(myViewWithTag.frame.origin.x, myViewWithTag.frame.origin.y, 768, 1024);

        // [myViewWithTag sizeToFit];
    }
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    // if the gesture recognizers are on different views, don't allow simultaneous recognition
    if (gestureRecognizer.view != otherGestureRecognizer.view)
        return NO;

    // if either of the gesture recognizers is the long press, don't allow simultaneous recognition
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
        return NO;

    return YES;
}

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:piece];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

        piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

来源:https://stackoverflow.com/questions/19858513/change-uiview-size-after-rotation-transform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!