UITouch not working for UIImageView

Discussion in 'iOS Programming' started by lamfan, Aug 4, 2008.

  1. macrumors newbie

    Joined:
    Aug 4, 2008
    #1
    Here is the source code, including 4 files

    inside the UIView instand touchesBegan method

    NSLog(@"touch"); <---working
    if([touch view] == view1)
    {
    NSLog(@"moving"); <---not working don't know why?
    }

    1.ShootingGameAppDelegate.h
    2.ShootingGameAppDelegate.m
    3.MainView.h
    4.MainView.m


    ShootingGameAppDelegate.h
    Code:
    #import <UIKit/UIKit.h>
    
    @class MainView;
    
    @interface ShootingGameAppDelegate : NSObject <UIApplicationDelegate> {
    	UIWindow *window;
    	MainView *mainView;
    }
    
    @property (nonatomic, retain) UIWindow *window;
    @property (nonatomic, retain) MainView *mainView;
    
    @end
    ShootingGameAppDelegate.m
    Code:
    #import "ShootingGameAppDelegate.h"
    #import "MainView.h"
    
    @implementation ShootingGameAppDelegate
    
    @synthesize window, mainView;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    	mainView = [[MainView alloc] initWithFrame:[window bounds]];
    	[window addSubview:mainView];
    	// Override point for customization after app launch	
        [window makeKeyAndVisible];
    }
    
    
    - (void)dealloc {
    	[window release];
    	[super dealloc];
    }
    
    
    @end
    MainView.h
    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface MainView : UIView {
    	UIImageView *view1;
    }
    
    @property (nonatomic, retain)UIImageView *view1;
    
    @end

    MainView.m

    Code:
    #import "MainView.h"
    
    @implementation MainView
    
    @synthesize view1;
    
    - (id)initWithFrame:(CGRect)frame {
    	if (self = [super initWithFrame:frame]) {
    		UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100.0, 100.0, 57.0, 57.0)];
    		imageView.image = [UIImage imageNamed:@"Icon.png"];
    		imageView.center = self.center;
    		self.view1 = imageView;
    		[imageView release];
    		[self addSubview:view1];
    	}
    	return self;
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	NSLog(@"touch");	
    	
    	UITouch *touch = [[event allTouches] anyObject];
    	if([touch view] == view1)
    	{
    		NSLog(@"moving");
    		CGPoint location = [touch locationInView:self];
    		view1.center = location;
    	}
    }
    
    - (void)dealloc {
    	[super dealloc];
    }
    
    @end
     
  2. macrumors member

    Joined:
    Jul 17, 2008
    #2
    I found this in documentation:
     
  3. thread starter macrumors newbie

    Joined:
    Aug 4, 2008

Share This Page