Touches: Move an Image to a certain extent?

Discussion in 'iOS Development' started by cocotutch, Jan 8, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Hey all,

    Just asking a question...I have code which allows me to move an image that covers half of the screen to the top/bottom. However, It can vanish below the screen, and when I move it, instead of using the edge, it moves via the centre co-ordinates.

    What I would really like to be able to do, is keep the image on screen in its entirety. Even after I "scroll" it up and down to reveal the two codes for my Combination Padlocks. So, have it unmovable when it hits the edge of the screen.

    I have the code if you need it, oh and it is moving along the Y-Axis only.

    Thanks in advance,

    cocotutch
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You can do any number of things, but the simplest would be to simply have the app stop moving the image.

    On your touchesMoved, which is how I presume you're moving the image, encompass the actual moving with an if statement.

    if (touch.location.y > = [UIScreen mainScreen] applicationFrame] + (yourImageView.frame.size.height / 2))
    [self moveImageToNewLocation];

    Do the same for the upper boundry.


    EDIT: Just to make sure, what I posted was pseudo code. You should be able to figure it out from what I posted.
  3. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Thanks skylar, But I don't really know where to put that code. This is what I have so far.

    Code:
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    {	
    	
    	
    	NSUInteger touchCount = 0;
    	
    	// Enumerates through all touch objects
    	for (UITouch *touch in touches){
    		// Send to the dispatch method, which will make sure the appropriate subview is acted upon
    	 	[self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self]];
    	    touchCount++;
    	}
    	
    	
    }
    Code:
    -(void) dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position {	
    	
    	CGPoint myPoint = CGPointMake(160,position.y); // I added this
    	// Check to see which view, or views,  the point is in and then move to that position.
    	if (CGRectContainsPoint([oldLockView frame], position)) {
    		oldLockView.center = myPoint; // I changed this
    		
    	}
    	
    	
    }
    I have the TouchesMoved event calling this function.
    I have edited the code you posted but I get loads of errors when I try to build it (SDK, non-toolchain). Do you know what I have done wrong?

    Thanks,

    cocotutch
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Something like this should take care of everything you want, in terms of moving your view:

    Code:
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	
    	UITouch *touch = [touches anyObject];
    	
    	if ([touch locationInView:self.window] > [[UIScreen applicationFrame] bounds].origin.y +[touch.view.frame.size.height / 2]) {
    		touch.view.center = [touch locationInView:self.window];
    	}
    }
  5. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Hey, thankyou again, but im still getting errors.

    I changed the code to:

    Code:
    UITouch *touch = [touches anyObject];
    	
    	if ([touch locationInView:self.window] > [[UIScreen applicationFrame] bounds].origin.y +[oldLockView.view.frame.size.height / 2]) {
    		oldLockView.view.center = [touch locationInView:self.window];
    	}
    With my view names and such, and smacked it into Touches Moved. Im still getting an error. "Syntax error before ']' token".

    I them changed it to:

    Code:
    UITouch *touch = [touches anyObject];
    	
    	if ([touch locationInView:self.window] > [[UIScreen mainScreen] bounds].origin.y +[oldLockView.frame.size.height / 2]) {
    		oldLockView.center = [touch locationInView:self.window];
    	}
    Removing the .view tokens after "oldLockView" gave me the "Syntax Error".

    I changed applicationFrame to mainScreen as I am using the SDK.

    Is this something to do with the SDK?

    Sorry 'bout all this,

    cocotutch

Share This Page