본문 바로가기

프로그래밍/아이폰 프로그래밍

2. 이미지 추가 및 터치 이벤트로 이동시키기 (UIImageView)

 

1. 이미지 프로젝트에 추가

Resources>Add>Existing Files...

 
 

2. xib 파일에 Image View 추가

Image - 보여질 사진으로 셋팅
Drawing>Opaque - png 투명도 셋팅 (체크시 불투명)
 
 

3. 코드 추가

HelloWorldViewController.h

#import <UIKit/UIKit.h>

 

@interface HelloWorldViewController : UIViewController {

IBOutlet UILabel *label_hello;

IBOutlet UIButton *button_iphone;

IBOutlet UIButton *button_ipad;

IBOutlet UIButton *button_ipodtouch;

IBOutlet UIImageView *image_bird; // 아웃렛 등록

}

 

- (IBAction) button1Touched;

- (IBAction) button2Touched;

- (IBAction) button3Touched;

 

@property (nonatomic, retain) IBOutlet UILabel *label_hello;

@property (nonatomic, retain) IBOutlet UIButton *button_iphone;

@property (nonatomic, retain) IBOutlet UIButton *button_ipad;

@property (nonatomic, retain) IBOutlet UIButton *button_ipodtouch;

@property (nonatomic, retain) IBOutlet UIImageView *image_bird; // 프로퍼티 등록

 

@end

 

 

 

HelloWorldViewController.m

 

#import "HelloWorldViewController.h"

 

@implementation HelloWorldViewController

 

@synthesize label_hello;

@synthesize button_iphone;

@synthesize button_ipad;

@synthesize button_ipodtouch;

@synthesize image_bird;

 

- (IBAction) button1Touched{

self.label_hello.text = @"Hello iPhone";

}

- (IBAction) button2Touched{

self.label_hello.text = @"Hello iPad";

}

- (IBAction) button3Touched{

self.label_hello.text = @"Hello iPod Touch";

}

 

// 이벤트 등록 및 구현

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

for(UITouch *touch in touches)

{

if(CGRectContainsPoint([image_bird frame],[touch locationInView:self.view]))

{

image_bird.center = [touch locationInView:self.view];

}

}

}

 

 

- (void)didReceiveMemoryWarning {

// Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

 

// Release any cached data, images, etc that aren't in use.

}

 

- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

 

 

- (void)dealloc {

[label_hello release];

[button_iphone release];

[button_ipad release];

[button_ipodtouch release];

[image_bird release]; // 리소스 해제

[super dealloc];

}

@end