2015-09-12 177 views
2

在我的iOS應用程序中,我使用UITouchGestureRecognizer拖放,縮放和旋轉UIImageView。我還將信息存儲在ImageView的最後位置,以便在應用程序重新啓動時檢索它。如何正確拖放,旋轉和縮放UIView?

這與iOS7的工作很好,但我最近開始使用iOS8進行測試,而且我正面臨一些問題,特別是使用縮放。每當imageview在旋轉參數中具有非0度角時,變焦不會像預期那樣起作用,並且會縮小圖像的大小。

我不明白爲什麼OS變化的確,這裏是我的代碼:

-(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指令CGAffineTransformScale,而不是之後減少它的大小,增加它。

我在哪裏做事錯了?

編輯:爲handleRotate

代碼
- (void)handleRotate:(UIRotationGestureRecognizer *)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; 
} 

回答

0

嘗試在ViewControllers .H做這些做這些..

第一..

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

現在.m文件做這些您的ViewController ..

- (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; 
} 

我希望它有幫助..

+0

我已經實現了該方法。請看我的編輯。 – Gannicus

+0

嘗試我的新更新代碼.. – krushnsinh

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

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) 
    } 
} 

希望,這是你在找什麼。任何擔心都會回到我身上。 :)