본문 바로가기

C/Objective C/ios

아이폰 Timer를 이용한 Animation 만들기 예제 따라하기

- 아이폰 Timer를 이용하여, Animation이 이루어 지는 것 처럼 Label을 움직여 보는 간단한 예제이다.

-화면 디자인

- Timer를 이용한 Animation 만들기 순서1. Xcode 실행

2. Single View Application
3. Project Name : “SimpleAni02”
4. storyboard에 Label 위치
5. Assistant Editor로 Label 드래그
6. “myLabel” 입력
7. “-(void)moveLabel:(NSTimer *)timer“ 입력 

8. ViewController.m 파일에 입력

-(void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval: 0.05 target:self

selector:@selector(moveLabel:) userIinfo:nil repeats:YES];

}
-(void)moveLabel:(NSTimer *)timer {

float wx = myLabel.center.x; float wy = myLabel.center.y; wx += 10;
if(320 < wx) {

wx = 0; }

wy += 5; if(480 < wy) {

wy = 0; }

myLabel.center = CGPointMake(wx, wy); }

9. 실행 


- ViewController.m 소스코드

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

@synthesize myLabel;


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveLabel:) userInfo:nil repeats:YES];

}


- (void)viewDidUnload

{

[self setMyLabel:nil];

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

} else {

    return YES;

}

}

float zz = 10;

-(void)moveLabel:(NSTimer *)timer{

float wx = myLabel.center.x

float wy = myLabel.center.y;

wx += 10;

if(320 < wx) {

wx = 0

zz+=10;

}

wy += 5

if(480 < wy) 

{

wy = 0

zz = 0;

}

myLabel.center = CGPointMake(wx, zz);

}

@end


- 실행화면