Rotating using didRotateFromInterfaceOrientation and transform UIImage afterwords

Discussion in 'iOS Programming' started by BarryK88, Mar 9, 2011.

  1. BarryK88, Mar 9, 2011
    Last edited by a moderator: Mar 9, 2011

    BarryK88 macrumors member

    Joined:
    Mar 1, 2011
    #1
    I got several images within my view. When i rotate my device I'd like to have my images being translated with the CGAffineTransformTranslate method so that the images are placed properly within my Landscape View. After I rotate back to Portrait the opposit translation needs to take place.

    The translation works so far. However the translation is also being done when I touch and drag the image. How can I make sure that the translation only works on rotation and not with touch and drag.

    Here's a part of my code so far:

    Code:
    -(void) touchesMoved: (NSSet *)touches withEvent: (UIEvent *) event {
    		
    		UITouch *touch = [[event allTouches] anyObject];
    		CGPoint location = [touch locationInView:self.view];
    		
    		
    		if ([touch view] == image1) {
    			
    			image.center = location;		
    			
    		}
    [super viewDidLoad];
    }
    
    -(void)didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation  {
    	
    	if(fromInterfaceOrientation == UIInterfaceOrientationPortrait ||
    	   fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    			image1.transform = CGAffineTransformTranslate(image1.transform, -64, 145);
    }
    
    if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
    	   fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    		image1.transform = CGAffineTransformTranslate(image1.transform, 64, -145);
    }
    
    Hope someone got an answer this is bothering me all day long. Thanks in advance
     
  2. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #2
    Why are you calling [super viewDidLoad] in your touchesMoved:withEvent: method? You only ever want to make that call in your UIViewController subclass' viewDidLoad and it should only be called once each time the view is loaded from a nib or after loadView is called.
     
  3. BarryK88 thread starter macrumors member

    Joined:
    Mar 1, 2011
    #3
    Hi thanks for your advice. I'm pretty new to the whole programming of Xcode...

    However I still can't figure out how to implement the right code for my needs. Strange thing is that when I'm in Portrait-view the entire drag & drop function works but when I change to Landscape-view the problems appears.
     
  4. BarryK88, Mar 10, 2011
    Last edited by a moderator: Mar 10, 2011

    BarryK88 thread starter macrumors member

    Joined:
    Mar 1, 2011
    #4
    Fixed this problem by using

    Code:
    if ([touch view] == image1) {
    		[COLOR="Red"]image1.transform = CGAffineTransformIdentity;[/COLOR]
    		image1.center = location;
    		
    	}
     

Share This Page