본문 바로가기

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

3. 슬라이더 추가하기 (UISlider)


1. 코드 등록
- Outlet 과 Action 을 추가한다.

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;

IBOutlet UISlider *slider_alpha; // 슬라이더 아웃렛 등록

}


- (IBAction) button1Touched;

- (IBAction) button2Touched;

- (IBAction) button3Touched;

- (IBAction) sliderChanged; // 슬라이더 이벤트 등록 


@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;

@property (nonatomic, retain) IBOutlet UISlider *slider_alpha; // 슬라이더 프로퍼티 등록


@end




HelloWorldViewcontroller.m

#import "HelloWorldViewController.h"


@implementation HelloWorldViewController


@synthesize label_hello;

@synthesize button_iphone;

@synthesize button_ipad;

@synthesize button_ipodtouch;

@synthesize image_bird;

@synthesize slider_alpha; // 슬라이더 통합 등록


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

}

-(IBAction) sliderChanged{ // 슬라이더 이벤트 구현

image_bird.alpha = slider_alpha.value;

}


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




2. HelloWorldViewController.xib에 슬라이더 추가


UISlider


Values : 최소값, 최대값 및 기본값 설정




3. Outlet 및 Action 연결


Action : Value Changed