我需要缩放和旋转我已经拖动我的UIView的自定义UITextView。我的UITextView周围有黑色边框。
问题在于,如果我用手势缩放UITextView然后旋转我的UITextView,文本会超出范围并且行为很奇怪。
有什么建议吗?
答案 0 :(得分:0)
~~ ZoomRotatePanTextView.h ~~
#import <UIKit/UIKit.h>
@interface ZoomRotatePanTextView : UITextView
- (void) reset:(BOOL)animation;
@end
~~ ZoomRotatePanTextView.m ~~
#import "ZoomRotatePanTextView.h"
@interface ZoomRotatePanTextView () <UIGestureRecognizerDelegate>
@property (nonatomic, retain) UIPinchGestureRecognizer *pinchRecogniser;
@property (nonatomic, retain) UIRotationGestureRecognizer *rotateRecogniser;
@property (nonatomic, retain) UIPanGestureRecognizer *panRecogniser;
@property (nonatomic, retain) UITapGestureRecognizer *tapRecogniser;
@property (nonatomic, retain) UITapGestureRecognizer *doubleTapRecogniser;
@property (nonatomic, retain) UILongPressGestureRecognizer *longPressRecogniser;
- (void) setUpGestures;
- (IBAction) handlePinch:(UIPinchGestureRecognizer*)recogniser;
- (IBAction) handleRotate:(UIRotationGestureRecognizer*)recogniser;
- (IBAction) handlePan:(UIPanGestureRecognizer*)recogniser;
- (IBAction) handleTap:(UITapGestureRecognizer*)recogniser;
- (IBAction) handleDoubleTap:(UITapGestureRecognizer*)recogniser;
- (IBAction) handleLongPress:(UITapGestureRecognizer*)recogniser;
@end
@implementation ZoomRotatePanTextView
#pragma mark - Initialisation Overrides
- (id) init {
self = [super init];
if (self) {
[self setUpGestures];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpGestures];
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setUpGestures];
}
return self;
}
- (void) setUpGestures {
[self setUserInteractionEnabled:TRUE];
_pinchRecogniser = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
_rotateRecogniser = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
_panRecogniser = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
_tapRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
_doubleTapRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
_longPressRecogniser = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[_pinchRecogniser setDelegate:self];
[_rotateRecogniser setDelegate:self];
[_panRecogniser setDelegate:self];
[_tapRecogniser setDelegate:self];
[_doubleTapRecogniser setDelegate:self];
[_longPressRecogniser setDelegate:self];
[self addGestureRecognizer:_pinchRecogniser];
[self addGestureRecognizer:_rotateRecogniser];
[self addGestureRecognizer:_panRecogniser];
[self addGestureRecognizer:_tapRecogniser];
[self addGestureRecognizer:_doubleTapRecogniser];
[self addGestureRecognizer:_longPressRecogniser];
_tapRecogniser.numberOfTapsRequired = 1;
_doubleTapRecogniser.numberOfTapsRequired = 2;
[_tapRecogniser requireGestureRecognizerToFail:_doubleTapRecogniser];
[self setContentMode:UIViewContentModeScaleAspectFit];
}
- (IBAction) handlePinch:(UIPinchGestureRecognizer*)recogniser {
recogniser.view.transform = CGAffineTransformScale(recogniser.view.transform, recogniser.scale, recogniser.scale);
recogniser.scale = 1;
}
- (IBAction) handleRotate:(UIRotationGestureRecognizer*)recogniser {
recogniser.view.transform = CGAffineTransformRotate(recogniser.view.transform, recogniser.rotation);
recogniser.rotation = 0;
}
- (IBAction) handleTap:(UITapGestureRecognizer*)recogniser {
}
- (IBAction) handleDoubleTap:(UITapGestureRecognizer*)recogniser {
[self reset:YES];
}
- (IBAction) handleLongPress:(UITapGestureRecognizer*)recogniser {
}
- (IBAction) handlePan:(UIPanGestureRecognizer*)recogniser {
if (recogniser.state == UIGestureRecognizerStateBegan || recogniser.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recogniser translationInView:self.superview];
CGPoint translatedCenter = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);
[self setCenter:translatedCenter];
[recogniser setTranslation:CGPointZero inView:self];
}
}
- (void) reset:(BOOL)animation {
if (!animation) {
self.transform = CGAffineTransformIdentity;
self.center = self.superview.center;
} else {
[UIView animateWithDuration:.25 animations:^{
self.transform = CGAffineTransformIdentity;
self.center = self.superview.center;
}];
}
}
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end