顯示具有 動畫實作 標籤的文章。 顯示所有文章
顯示具有 動畫實作 標籤的文章。 顯示所有文章

2012年2月23日 星期四

鋼鐵人的HUD實作

從我開始寫APP的第一天開始,我就夢想著有一天可以做出像是關鍵報告或是鋼鐵人那種充滿未來性、科技感的操作界面,但是說實在的,也不是這麼難,這裡就來示範一下怎麼做實作出這麼酷的UI。



















首先,先要把這基本的HUD(抬頭顯示器?)畫出來,在這裡有教學網頁教怎麼用photoshop畫出類似的風格。我是沒照著那裡教的畫,直接看著上面這張電影裡的截圖畫了幾個圈示意一下。

雖然在那個教學網站裡,教你畫出這一堆圓圈後,他的工作就完成了,但是,對我們來說,不是只是要“看”起來像鋼鐵人的畫面,我們是要讓他“用”起來像鋼鐵人用的才酷。

所以,先把剛才畫的一堆圈圈拆成個別的一個一個的圈圈,我是拆成5個圈圈,分別存成5個PNG檔(如下圖)。分別命名為circle1Image到circle5Image。實作方式請看下列程式說明。











先在.h宣告如下:

//4個圈圈的UIImageView


    IBOutlet UIImageView *circle1Image;
    IBOutlet UIImageView *circle2Image;
    IBOutlet UIImageView *circle3Image;
    IBOutlet UIImageView *circle5Image;

//4個圈圈的CGAffineTransform

    CGAffineTransform circle1Transform;
    CGAffineTransform circle2Transform;
    CGAffineTransform circle3Transform;
    CGAffineTransform circle5Transform;

然後在.m實作

在initWithNibName裡先宣告一個NSTimer可以讓圈圈自已轉起來

//每0.03秒執行一次selfMoveCircle這個程序

timer =[NSTimer scheduledTimerWithTimeInterval:0.03f 
target:self
                              selector:@selector(selfMoveCircle)
      userInfo:nil
       repeats:YES];

//在這個selfMoveCircle裡只讓3個圈圈自已會一直轉,另外2個圈圈是要聽令於我們的指揮,當作操作控制的轉盤。

-(void)selfMoveCircle{
    circle5Transform = circle5Image.transform;
    circle3Transform = circle3Image.transform;
    circle2Transform = circle2Image.transform;
    //把3個圈轉動的速度設成不同,感覺比較不會這麼死板
    circle5Image.transform = CGAffineTransformRotate(circle5Transform, 0.02);
    circle3Image.transform = CGAffineTransformRotate(circle3Transform, 0.02);
    circle2Image.transform = CGAffineTransformRotate(circle2Transform, 0.04);
}

//當我們轉動轉盤的時侯,最外圍的圈circle1Image, 跟著我們轉動的方向反方向轉動。

- (void)controlCircleMove:(NSInteger)wheelCV{
    if (wheelCV >0) {
        circle1Transform = circle1Image.transform;
        circle1Image.transform = CGAffineTransformRotate(circle1Transform, -0.6);
    }else {
        circle1Transform = circle1Image.transform;
        circle1Image.transform = CGAffineTransformRotate(circle1Transform, 0.6);
    }
}

耶...怎麼沒宣告和實作到circle4Image呢?因為circle4Image是我們主要的操控盤,也就是我們手指怎麼轉,它就跟著怎麼轉,所以它是要另外再宣告在一個wheelView.h,並實作在wheelView.m負責控管我們手指的轉動方向,並把轉動的值傳回到這裡來告訴circle1Image要怎麼轉。但是實作轉輪又是一個大工程,另外再寫一篇怎麼實作好了。

2012年2月3日 星期五

UIView 同時移動、旋轉、放大的作法

在orderGear裡有個我很喜歡的動畫效果,就是如果點選左手邊的菜色相片,它就會移動到畫面的中央並放大、轉正。
點選前:

點選後:

這裡來示範一下怎麼用setAnimation來實作這樣的效果。

先在.h宣告

IBOutlet UIView *photoframeView_4;


因為這張看似拍立得的相片中包含了相片、框、菜名、按揵等好幾個物件,所以設定為UIView來包含所有物件, 而不是用UIImageView, 如果只有一張圖的時侯還是可以用UIImageView。

然後在.m實作一個IBAction,就是當按下這張相片時的動作。

-(IBAction) showEnlargePhotoframe:(id)sender{
   CGPoint photoframe_4CurrentPOS = photoframeView_4.center;
photoframe_4CurrentPOS.x = 142; //移動前的x座標
photoframe_4CurrentPOS.y = 573; //移動前的y座標
photoframeView_4.center = photoframe_4CurrentPOS;
float angle_4  = -14*(PI/100); //原來旋轉的角度
CGAffineTransform transformRotate_4 = CGAffineTransformMakeRotation(angle_4);
CGAffineTransform transform_4 = CGAffineTransformScale(transformRotate_4, 0.36, 0.36); //原來的大小(原本尺寸縮到36%的大小)

[photoframeView_4 setTransform:transform_4];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

photoframeView_4.hidden = NO;
enlargeButton_4.hidden = YES; //隱藏放大的按鍵
normalButton_4.hidden = NO; //顯示放大後的按鍵
photoframe_4CurrentPOS.x = 512//移動後的x座標

photoframe_4CurrentPOS.y = 374//移動後的y座標

photoframeView_4.center = photoframe_4CurrentPOS;
angle_4  = 0*(PI/100); //旋轉UIView成正的
transformRotate_4 = CGAffineTransformMakeRotation(angle_4);
transform_4 = CGAffineTransformScale(transformRotate_4, 1, 1); //放大回原本大小
[photoframeView_4 setTransform:transform_4];
[UIView commitAnimations]; //執行動畫
}

所以,與其說是點選後開始變型(transform), 不如說是讓原本又小又歪的圖在點選後回復成原本的角度和大小。

2012年1月31日 星期二

『向下拉』提示動畫實作

在遊戲的進行中,雖然讓玩家自行摸索過關的技巧是玩遊戲的一種樂趣,但是我們又不希望玩家卡在一個關卡後,就失去了繼續玩下去的興緻了,所以在樂田麵包屋遊戲中,有很多的操作需要有提示才能讓玩家可以順利的過關。這裡示範一個向下拉動畫的實作。



要完成這樣的一個動畫,可以有2種不同的方法,你可以用setAnimation的方法,也可以用animationImages的方法,如果用setAnimation的方法只要準備2張圖片,一張是向下的箭頭,另一張是手指,然後將箭頭的圖片固定,使用setAnimation的方法讓手指的圖片移動y座標,這樣就可以產生手指向下滑動的動畫。

但是我們今天示範另一個用animationImages的作法:首先,如上圖準備5張分格的圖,分別命名為dragDown_1.png到dragDown_5.png。然後在.h宣告一個

UIImage *dragDown

接著在.m實作dragDownTip的程序


- (void)dragDownTip{
     dragDown.hidden = NO;
     dragDown.animationImages = [NSArray arrayWithObjects:
                            [UIImage imageNamed:@"dragDown_1.png"],
                            [UIImage imageNamed:@"dragDown_2.png"],
                            [UIImage imageNamed:@"dragDown_3.png"],
                            [UIImage imageNamed:@"dragDown_4.png"],
                            [UIImage imageNamed:@"dragDown_5.png"], nil];
     [dragDown setAnimationRepeatCount:3];
     dragDown.animationDuration = 1.5;
     [dragDown startAnimating];
}

當要執行動畫的時侯, 只要下指令:

[self dragDownTip];

就可以了。真是比setAnimation麻煩多了...當初我怎麼沒想到要用setAnimation的方法呢?真是不經一事,不長一智...

2012年1月27日 星期五

『樂田麵包屋』過關畫面分解

在樂田麵包屋遊戲中,每一次過關的時侯,都會有一段6秒鐘的過關畫面。雖然只有短短的6秒鐘,但是裡面包含了不算簡單的工作。在此將這些動作分解讓大家瞭解一下。




















這個畫面雖然只包含了4個元素(從上圖左手邊的objects可以看得出來),一張背景,一個小廚師的圖,一個獎牌,和小廚師身邊的星星。

實際上這6秒的畫面包含了以下的畫面:

1張背景


2張小廚師




- (void)chefAnimate{
    
    animationChef.animationImages = [NSArray arrayWithObjects:
                                    [UIImage imageNamed:@"chef_verygood1.png"],
                                    [UIImage imageNamed:@"chef_verygood2.png"],
                                     nil];
    
    [animationChef setAnimationRepeatCount:10];
    animationChef.animationDuration = 1.5;
    [animationChef startAnimating];
    
    
}


5張獎牌不同光澤


- (void)verygoodAnimate{
    
    animationVerygood.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"medalVerygood1.png"],
                                         [UIImage imageNamed:@"medalVerygood2.png"],
                                         [UIImage imageNamed:@"medalVerygood3.png"],
                                         [UIImage imageNamed:@"medalVerygood4.png"],
                                         [UIImage imageNamed:@"medalVerygood5.png"],
                                         [UIImage imageNamed:@"medalVerygood1.png"],nil];
    
    [animationVerygood setAnimationRepeatCount:10];
    animationVerygood.animationDuration = 1;
    [animationVerygood startAnimating];
    
    
}


1張閃爍不停的星星
- (void)starAnimation{
    starImage.alpha = 0;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    starImage.alpha = 1.0;
    [UIView commitAnimations];
}




2012年1月6日 星期五

用setAnimation呈現動畫


- (void)showLines{
    lines.hidden = NO; //設定原本是隱藏
    CGPoint Lpos = lines.center;
    Lpos.y = -12.0f; //原本的位置是y座標在-12
    lines.center = Lpos;
    //這裡以上是動以前的位置
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    //這裡以下是預計要動的目的地
    Lpos.y = 10.0f; //讓要移動的物件從-12移到y座標10, 也就是垂直往下移動
    lines.center = Lpos;
    [UIView commitAnimations]; //執行移動的指令
}

2012年1月3日 星期二

用animationImages呈現動畫

-(void)showGood{
    chefAnimation.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"chef_good1.png"], //動畫由哪些圖片組成
                                     [UIImage imageNamed:@"chef_good2.png"],
                                     [UIImage imageNamed:@"chef_good2.png"],
                                     [UIImage imageNamed:@"chef_good2.png"],
                                     [UIImage imageNamed:@"chef_good2.png"],
                                     nil];
    
    [chefAnimation setAnimationRepeatCount:3]; //動畫重覆播放的次數
    chefAnimation.animationDuration = 3; //動畫每一輪的時間(秒)
    [chefAnimation startAnimating]; //開始執行動畫
}