我的代码需要专业 xcoder 的帮助 :-)
我有一个名为“headline”的可拖动uitextview,它是“mainstage”中的子视图。我还添加了一个捏合手势来更改 uitextview 内的字体大小。一切正常,但我真正需要的最后一个基本功能是 uitextview 的旋转。
所以我添加了一个用于旋转和 volia 的 uislider!它也有效。但是在这种转换之后,当通过捏合手势变成新尺寸时,uitextview 会变得疯狂*。(*uitextview的位置和大小)
在对文档进行一些研究后,我发现“一些有用的警告”(框架):
如果您希望在框架矩形更改时调用 drawRect: 方法,请将 contentMode 属性设置为 UIViewContentModeRedraw
很好......但我不知道我必须把它放在我的代码中......
换句话说:如何在旋转后更改 uiview 的框架大小?(myview.transform)
感谢大家对此的帮助……</p>
这是我的代码:
- (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.
}