如何正确拖放,旋转和缩放UIView?

时间:2015-09-12 12:11:21

标签: ios objective-c uiview

在我的iOS应用程序中,我使用UITouchGestureRecognizer来拖放,缩放和旋转UIImageView。我还将信息存储在ImageView的最后位置,以便在应用重新启动时检索它。

这适用于iOS7,但我最近开始使用iOS8进行测试,我遇到了一些问题,尤其是缩放问题。每当图像视图在旋转参数中具有非0角度时,缩放就不会像预期的那样起作用,并且会缩小图像的大小。

我不明白操作系统更改的原因,这是我的代码:

PdfReader reader2 = new PdfReader(src);         

        int n = reader2.getNumberOfPages();
        PdfDictionary pageDict;
        PdfRectangle rect = new PdfRectangle(305, 152,  532 , 227); //3.15 x 1.04 in inch
        for (int i = 1; i <= n; i++) {
            pageDict = reader2.getPageN(i);
            pageDict.put(PdfName.CROPBOX, rect);
        }
        PdfStamper stamper = new PdfStamper(reader2, new FileOutputStream(src2));

        stamper.close();
        reader2.close();

我想我进行转换的方式是错误的,因为当我在-(void) viewDidAppear:(BOOL)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 action:@selector(handlePan:)]; panRecognizer.delegate = self; [imageView addGestureRecognizer:panRecognizer]; UIRotationGestureRecognizer * rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)]; rotationRecognizer.delegate = self; [imageView addGestureRecognizer:rotationRecognizer]; UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; pinchRecognizer.delegate = self; [imageView addGestureRecognizer:pinchRecognizer]; [self.view addSubview:imageView]; } } 之后放置CGAffineTransformMakeRotation指令而不是减小尺寸时,它会增加它。

我在哪里做错了?

编辑:handleRotate的代码

CGAffineTransformScale

3 个答案:

答案 0 :(得分:1)

首先,您需要让视图控制器实现UIGestureRecognizerDelegate。

然后将手势识别器添加到您的UIView(我看到您已经这样做了,但只是将它放在这里以便将解决方案放在一个地方)。

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panGesture.delegate = self;
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
rotationGesture.delegate = self;
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
pinchGesture.delegate = self;

[yourView addGestureRecognizer:panGesture];
[yourView addGestureRecognizer:rotationGesture];
[yourView addGestureRecognizer:pinchGesture];

然后,这就是您的handle...方法应该如何:

- (void)handlePinch:(UIPinchGestureRecognizer *)pinchRecognizer
{
    UIGestureRecognizerState state = [pinchRecognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGFloat scale = [pinchRecognizer scale];
        [pinchRecognizer.view setTransform:CGAffineTransformScale(pinchRecognizer.view.transform, scale, scale)];
        [pinchRecognizer setScale:1.0];
    }
}


- (void)handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer {

    UIGestureRecognizerState state = [rotationGestureRecognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGFloat rotation = [rotationGestureRecognizer rotation];
        [rotationGestureRecognizer.view setTransform:CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation)];
        [rotationGestureRecognizer setRotation:0];
    }
}

- (void)handlePan:(UIPanGestureRecognizer *)panRecognizer {

    UIGestureRecognizerState state = [panRecognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [panRecognizer translationInView:panRecognizer.view];
        [panRecognizer.view setTransform:CGAffineTransformTranslate(panRecognizer.view.transform, translation.x, translation.y)];
        [panRecognizer setTranslation:CGPointZero inView:panRecognizer.view];
    }
}

这就是它的样子:

enter image description here

答案 1 :(得分:0)

尝试做这些..

首先在你的ViewControllers .h做这些..

@interface RotationViewController : UIViewController<UIGestureRecognizerDelegate>//Add these..
{
    UIView *testView;
}

现在在ViewController的.m文件中执行这些操作..

- (void)viewDidLoad
{
    [super viewDidLoad];
   // Do any additional setup after loading the view.

    testView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 160, 160)];

    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];
    [imageView setImage:[UIImage imageNamed:@"ImageName.png"]];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];

    [testView addSubview:imageView];
    [self.view addSubview:testView];

    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
    [testView addGestureRecognizer:rotationGestureRecognizer];
}

-(void) handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
    testView.transform = CGAffineTransformRotate(testView.transform, rotationGestureRecognizer.rotation);
    rotationGestureRecognizer.rotation = 0.0;
}

我希望它有所帮助......

答案 2 :(得分:0)

首先,您需要让视图控制器实现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)
    }
}

希望,这是你正在寻找的。任何关注都会回复给我。 :)