在我的 iOS 应用程序中,我使用 UITouchGestureRecognizer 拖放、缩放和旋转 UIImageView。我还将信息存储在 ImageView 的最后位置,以便在应用重新启动时检索它。
这在 iOS7 上运行良好,但我最近开始在 iOS8 上进行测试,但遇到了一些问题,尤其是在缩放方面。每当 ImageView 在旋转参数中具有非 0 角度时,缩放就不会像预期的那样起作用并减小图像的大小。
我不明白操作系统为什么会这样,这是我的代码:
-(void) viewDidAppearBOOL)animated{
[super viewDidAppear:YES];
[self loadAllImages];
for (GIFavorite *favorite in self.favorites){
UIImage *image = favorite.image;
ImageView *imageView = [[ImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageView.center = CGPointMake(favorite.x, favorite.y);
imageView.transform = CGAffineTransformMakeRotation(favorite.rotation);
imageView.transform = CGAffineTransformScale(imageView.transform, favorite.scale, favorite.scale);
imageView.image = image;
imageView.userInteractionEnabled = YES;
UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self actionselector(handlePan];
panRecognizer.delegate = self;
[imageView addGestureRecognizer:panRecognizer];
UIRotationGestureRecognizer * rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self actionselector(handleRotate];
rotationRecognizer.delegate = self;
[imageView addGestureRecognizer:rotationRecognizer];
UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self actionselector(handlePinch];
pinchRecognizer.delegate = self;
[imageView addGestureRecognizer:pinchRecognizer];
[self.view addSubview:imageView];
}
}
我猜我进行转换的方式是错误的,因为当我将 CGAffineTransformMakeRotation
指令放在 CGAffineTransformScale
之后而不是减小大小时,它会增加它。
我哪里做错了?
编辑:handleRotate 的代码
- (void)handleRotateUIRotationGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
ImageView *imageView = recognizer.view;
CGFloat radians = atan2f(imageView.transform.b, imageView.transform.a);
favorite.rotation = radians;
recognizer.rotation = 0;
}
首先,你需要让你的 View Controller 实现 UIGestureRecognizerDelegate。
SWIFT 3:-
func setUpGestures () {
let panGesture = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(gesture))
self.imageView.addGestureRecognizer(panGesture)
let rotationGesture = UIRotationGestureRecognizer(target: self, action:#selector(self.handleRotationGesture(gesture))
self.imageView.addGestureRecognizer(rotationGesture)
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.handlePinchGesture(gesture))
self.imageView.addGestureRecognizer(pinchGesture)
}
func handlePinchGesture(gesture: UIPinchGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIPinchGestureRecognizer")
gesture.view?.transform = (gesture.view?.transform)!.scaledBy(x: gesture.scale, y: gesture.scale)
gesture.scale = 1.0
}
}
func handleRotationGesture(gesture: UIRotationGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIRotationGestureRecognizer")
gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation)
gesture.rotation = 0
}
}
func handlePanGesture(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIPanGestureRecognizer")
let translation = gesture.translation(in: view)
gesture.view?.transform = (gesture.view?.transform)!.translatedBy(x: translation.x, y: translation.y)
gesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
}
}
希望,这就是您要找的。任何问题都可以回复我。
关于ios - 如何正确拖放、旋转和缩放 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32538629/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |