C/Objective C/ios2012.07.18 10:36

- 아이폰 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


- 실행화면




신고
Posted by Namseungil
C/Objective C/ios2012.07.17 13:11

- UIView 클래스에 있는 Animation 기능을 사용하여 간단한 예제이다.

- Button을 누르면 Label이 Animation 기능으로 움직이게 된다.


- 화면 디자인


- Animation 만들기 순서

1. Xcode 실행
2. Single View Application
3. Project Name : “SimpleAni01”
4. storyboard에 Button과 Label 위치
5. Assistant Editor로 Label 드래그
6. “myLabel” 입력
7. Button을 Assistant Editor로 드래그 Connection을 Action으로 변경 “tapBtn” 입력 

8. ViewController.m 파일에 입력

-(IBAction) tapBtn {
myLabel.center = CGPointMake(50, 100); //숫자는 좌표!
[UIView beginAnimations: nil context:nil];
[UIView setAnimationDuration: 2];
myLabel.center = CGPointMake(160, 240); //숫자는 좌표!!
myLabel.transform = CGAffineTransformMakeRotation(270 * M_PI / 180); [UIView commitAnimations];

}

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.

}


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

}

}


- (IBAction)tapBtn:(id)sender {

myLabel.center = CGPointMake(100, 100);

[UIView beginAnimations: nil context:nil];

[UIView setAnimationDuration: 1];

myLabel.center = CGPointMake(200, 200);

myLabel.transform = CGAffineTransformMakeRotation(240 * M_PI / 180); [UIView commitAnimations];

}

@end

- 실행화면


신고
Posted by Namseungil