第一個(gè)項(xiàng)目之六--手勢(shì)和動(dòng)畫

前言


上篇是總算把地鐵線給畫出來了篓冲,雖然沒什么美感。嘛瘪弓,那些可以慢慢改的嘛垫蛆。
這篇就來試著給地鐵線加些炫酷到不行的功能(其實(shí)就是點(diǎn)擊動(dòng)畫)。
<br />

iOS的事件


在iOS里面腺怯,動(dòng)作事件有三類:
1.屏幕觸摸事件(點(diǎn)擊袱饭、長(zhǎng)按、拖動(dòng)等)
2.加速器動(dòng)作事件(搖一搖呛占、橫向放置等)
3.遠(yuǎn)程控制事件(耳機(jī)按鈕虑乖、外置手柄等)
所有繼承了UIResponder的類都可以對(duì)事件進(jìn)行處理。
這里只對(duì)屏幕觸摸事件做一個(gè)實(shí)際應(yīng)用測(cè)試晾虑,下面就復(fù)制一段對(duì)應(yīng)事件方法:

Paste_Image.png

<br />

加上標(biāo)簽


因?yàn)榇蛩銓?duì)站點(diǎn)添加觸摸動(dòng)作疹味,但是站點(diǎn)都是局部變量,也就是初始化完了之后帜篇,我就丟失了聯(lián)系他們的辦法了糙捺。那么,此時(shí)我們要做的坠狡,就是先給每個(gè)局部變量加上一個(gè)標(biāo)記继找,然后再添加一個(gè)手勢(shì)識(shí)別,這樣我們就可以通過標(biāo)記來響應(yīng)觸摸事件逃沿,來找到對(duì)應(yīng)的站點(diǎn)了婴渡。
首先我要先加兩個(gè)變量,這是為后面做動(dòng)畫的時(shí)候準(zhǔn)備的:

@property (nonatomic, strong) NSMutableArray *array_label_station;
@property (nonatomic, strong) UIView *view_background_route;

然后先初始化一個(gè)單擊手勢(shì)凯亮,并將手勢(shì)添加到站點(diǎn)上面边臼,再設(shè)置一個(gè)標(biāo)記:

UITapGestureRecognizer *gesture_tap_temp1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapAction:)];
lb_start.tag = 90000;
lb_start.userInteractionEnabled = YES;
[lb_start addGestureRecognizer:gesture_tap_temp1];

大概效果最后就是:

RouteViewGesture.gif

很渣的動(dòng)畫


手勢(shì)動(dòng)作已經(jīng)做好了,是時(shí)候加一下很酷(zha)的動(dòng)畫效果了假消。
其實(shí)在AlertMessage里面就有用到動(dòng)畫柠并,在這里也是在AlertMessage的動(dòng)畫基礎(chǔ)上做一個(gè)改動(dòng)而已。
首先富拗,要先加一個(gè)變量臼予,是用來判斷動(dòng)畫是否已經(jīng)執(zhí)行了的。

@property (nonatomic, assign) BOOL isStationsPullAside;

首先對(duì)剛才的那個(gè)手勢(shì)方法做個(gè)改動(dòng)啃沪,那個(gè)showAlertMessage就扔掉了吧粘拾,沒有用了。我們換一個(gè)炫酷很多的:

- (void)singleTapAction:(UITapGestureRecognizer *)sender{
    [self pullStationsAsideWithTag:sender.self.view.tag];
}

- (void)pullStationsAsideWithTag:(long)tag{
    float alpha_route = 0;
    float duration_route = 0.2f;
    if(self.isStationsPullAside){
        alpha_route = 1;
        duration_route = 0.4f;
    }
    else{
        alpha_route = 0;
        duration_route = 0.2f;
    }
    [UIView animateWithDuration:duration_route animations:^{
        [self.view_background_route setAlpha:alpha_route];
    }];
    
    for(UILabel *lb_temp in self.array_label_station){
        if(self.isStationsPullAside){
            [UIView animateWithDuration:0.25f animations:^{
                [lb_temp setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:lb_temp.frame.origin.y andSize:lb_temp.frame.size]];
            }];
        }else{
            [UIView animateWithDuration:0.25f animations:^{
                [lb_temp setFrame:CGRectMake(0 - ((lb_temp.frame.size.width / 4) * 3), lb_temp.frame.origin.y, lb_temp.frame.size.width, lb_temp.frame.size.height)];
            }completion:^(BOOL finished){
                if(lb_temp.tag == tag){
                    [UIView animateWithDuration:0.15f animations:^{
                        [lb_temp setFrame:CGRectMake(0 - ((lb_temp.frame.size.width / 6) * 2), lb_temp.frame.origin.y, lb_temp.frame.size.width, lb_temp.frame.size.height)];
                    }];
                }
            }];
        }
    }
    self.isStationsPullAside = !self.isStationsPullAside;
}

這就完成了创千,先來看看效果缰雇,不要抱太多期望入偷,渣到我自己都在偷笑!

RouteViewAnimate.gif

動(dòng)畫就是這樣械哟,在動(dòng)畫執(zhí)行完之后疏之,右邊有好大一片空白,我們當(dāng)然是要充分利用這片空白的暇咆!我們就在上面添加上附近景點(diǎn)啊锋爪、公交路線啊、簡(jiǎn)介啊糯崎、圖片啊什么鬼的几缭。
其實(shí)就是添加多一個(gè)UIView河泳,在點(diǎn)擊之后先設(shè)置Alpha為0沃呢,然后用動(dòng)畫將Alpha改為1。這樣就搞定了:

- (void)pullStationsAsideWithTag:(long)tag{
    CGSize size_dialog = CGSizeMake(200, self.view.frame.size.height - HEIGHT_NAVIGATIONBAR_STATUSBAR - (GAP_DEFAULT * 4));

    float alpha_route = 0;
    float duration_route = 0.2f;
    if(self.isStationsPullAside){
        alpha_route = 1;
        duration_route = 0.4f;
    }
    else{
        self.view_dialog = [[UIView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - 55, HEIGHT_NAVIGATIONBAR_STATUSBAR + (GAP_DEFAULT * 2), size_dialog.width, size_dialog.height)];
        self.view_dialog.backgroundColor = [CommonHandler getColorWithRGB:0xf7ef96];
        [self.view_dialog setAlpha:0];
        [self.view addSubview:self.view_dialog];
        alpha_route = 0;
        duration_route = 0.2f;
    }
    [UIView animateWithDuration:duration_route animations:^{
        [self.view_background_route setAlpha:alpha_route];
    }];
    
    for(UILabel *lb_temp in self.array_label_station){
        if(self.isStationsPullAside){
            [UIView animateWithDuration:0.25f animations:^{
                [self.view_dialog setAlpha:0];
                [lb_temp setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:lb_temp.frame.origin.y andSize:lb_temp.frame.size]];
            }completion:^(BOOL finished){
                [self.view_dialog removeFromSuperview];
            }];
        }else{
            [UIView animateWithDuration:0.25f animations:^{
                [lb_temp setFrame:CGRectMake(0 - ((lb_temp.frame.size.width / 4) * 3), lb_temp.frame.origin.y, lb_temp.frame.size.width, lb_temp.frame.size.height)];
            }completion:^(BOOL finished){
                if(lb_temp.tag == tag){
                    [UIView animateWithDuration:0.15f animations:^{
                        [lb_temp setFrame:CGRectMake(0 - ((lb_temp.frame.size.width / 7) * 2), lb_temp.frame.origin.y, lb_temp.frame.size.width, lb_temp.frame.size.height)];
                        [self.view_dialog setAlpha:1];
                    }];
                }
            }];
        }
    }
    self.isStationsPullAside = !self.isStationsPullAside;
}

最后貼上效果拆挥,至于怎么添加內(nèi)容嘛薄霜,有興趣的可以自己去嘗試!

RouteViewPopover.gif

整合代碼


然后放上今天的所有代碼纸兔!

#import "RouteViewController.h"
#import "CommonHandler.h"
#import "AlertMessage.h"

#define GAP_DEFAULT 10

@interface RouteViewController ()

@property (nonatomic, strong) NSNumber *number_station;

@property (nonatomic, strong) NSArray *array_station;
@property (nonatomic, strong) NSMutableArray *array_label_station;

@property (nonatomic, strong) UIView *view_background_route;
@property (nonatomic, strong) UIView *view_dialog;

@property (nonatomic, assign) BOOL isStationsPullAside;

@end

@implementation RouteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initRouteView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initRouteView{
    self.view.backgroundColor = [CommonHandler getColorWithRed:235 andGreen:235 andBlue:235 andAlpha:1];
    
    [self initData];
}

- (void)initData{
    self.array_station = [NSArray arrayWithObjects:@"起點(diǎn)", @"第一站", @"第二站", @"第三站", @"第四站", @"終點(diǎn)", nil];
    self.number_station = [NSNumber numberWithLong:[self.array_station count] - 2];
    self.array_label_station = [[NSMutableArray alloc]init];

    self.isStationsPullAside = NO;
    
    [self drawRoute];
}

- (void)drawRoute{
    self.view_background_route = [[UIView alloc]initWithFrame:self.view.bounds];
    self.view_background_route.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.view_background_route];
    
    CGSize size_label = CGSizeMake(120, 40);
    
    UITapGestureRecognizer *gesture_tap_temp1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapAction:)];
    
    UILabel *lb_start = [[UILabel alloc]init];
    
    lb_start.text = self.array_station[0];
    lb_start.textColor = [UIColor whiteColor];
    lb_start.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_start.textAlignment = NSTextAlignmentCenter;
    
    lb_start.layer.backgroundColor = [CommonHandler getColorWithRGB:0x18aaf2].CGColor;
    lb_start.layer.cornerRadius = 20.0;
    [lb_start.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:(HEIGHT_NAVIGATIONBAR_STATUSBAR + GAP_DEFAULT) andSize:size_label]];
    lb_start.tag = 90000;
    lb_start.userInteractionEnabled = YES;
    [lb_start addGestureRecognizer:gesture_tap_temp1];
    [self.view addSubview:lb_start];
    [self.array_label_station addObject:lb_start];
    
    UITapGestureRecognizer *gesture_tap_temp2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapAction:)];
    
    UILabel *lb_end = [[UILabel alloc]init];
    
    lb_end.text = [self.array_station lastObject];
    lb_end.textColor = [UIColor whiteColor];
    lb_end.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_end.textAlignment = NSTextAlignmentCenter;
    
    lb_end.layer.backgroundColor = [UIColor redColor].CGColor;
    lb_end.layer.cornerRadius = 20.0;
    [lb_end.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:self.view.frame.size.height - GAP_DEFAULT - size_label.height andSize:size_label]];
    lb_end.tag = 90000 + [self.array_station count] - 1;
    lb_end.userInteractionEnabled = YES;
    [lb_end addGestureRecognizer:gesture_tap_temp2];
    [self.view addSubview:lb_end];
    
    CGFloat height_middle = (lb_end.frame.origin.y + (lb_end.frame.size.height / 2)) - (lb_start.frame.origin.y + (lb_start.frame.size.height / 2));
    CGFloat gap_station = height_middle / ([self.number_station intValue] + 1);
    
    for(int i = 0; i < [self.number_station intValue]; i++){
        UILabel *lb_temp = [[UILabel alloc]init];
        lb_temp.text = self.array_station[i + 1];
        lb_temp.textAlignment = NSTextAlignmentCenter;
        lb_temp.textColor = [UIColor whiteColor];
        lb_temp.font = [UIFont fontWithName:@"Arial" size:15.0];
        
        lb_temp.layer.backgroundColor = [UIColor orangeColor].CGColor;
        lb_temp.layer.cornerRadius = 15.0;
        [lb_temp.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:((lb_start.frame.origin.y + (lb_start.frame.size.height / 2)) + (gap_station * (i + 1))) - (size_label.height / 2) andSize:size_label]];
        
        [self.view addSubview:lb_temp];
        
        lb_temp.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *gesture_tap_temp = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapAction:)];
        
        lb_temp.tag = 90000 + i + 1;
        [lb_temp addGestureRecognizer:gesture_tap_temp];
        [self.array_label_station addObject:lb_temp];
    }
    [self.array_label_station addObject:lb_end];
    
    CGSize size_route = CGSizeMake(5, 5);
    CGFloat height_route = gap_station - size_label.height;
    CGFloat gap_route = height_route / size_route.height;
    
    for(int i = 0; i < [self.number_station intValue] + 1; i++){
        for(int j = 0; j < (int)gap_route; j++){
            UILabel *lb_temp = [[UILabel alloc]init];
            if(j % 2 == 0){
                lb_temp.backgroundColor = [UIColor blackColor];
            }else{
                lb_temp.backgroundColor = [UIColor yellowColor];
            }
            [lb_temp setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:(lb_start.frame.origin.y + lb_start.frame.size.height + ((height_route + size_label.height) * i) + (size_route.height * j)) andSize:size_route]];
            [self.view_background_route addSubview:lb_temp];
        }
    }
}

- (void)singleTapAction:(UITapGestureRecognizer *)sender{
    [self pullStationsAsideWithTag:sender.self.view.tag];
}

- (void)pullStationsAsideWithTag:(long)tag{
    CGSize size_dialog = CGSizeMake(200, self.view.frame.size.height - HEIGHT_NAVIGATIONBAR_STATUSBAR - (GAP_DEFAULT * 4));

    float alpha_route = 0;
    float duration_route = 0.2f;
    if(self.isStationsPullAside){
        alpha_route = 1;
        duration_route = 0.5f;
    }
    else{
        self.view_dialog = [[UIView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - 55, HEIGHT_NAVIGATIONBAR_STATUSBAR + (GAP_DEFAULT * 2), size_dialog.width, size_dialog.height)];
        self.view_dialog.backgroundColor = [CommonHandler getColorWithRGB:0xf7ef96];
        [self.view_dialog setAlpha:0];
        [self.view addSubview:self.view_dialog];
        alpha_route = 0;
        duration_route = 0.2f;
    }
    [UIView animateWithDuration:duration_route animations:^{
        [self.view_background_route setAlpha:alpha_route];
    }];
    
    for(UILabel *lb_temp in self.array_label_station){
        if(self.isStationsPullAside){
            [UIView animateWithDuration:0.25f animations:^{
                [self.view_dialog setAlpha:0];
                [lb_temp setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:lb_temp.frame.origin.y andSize:lb_temp.frame.size]];
            }completion:^(BOOL finished){
                [self.view_dialog removeFromSuperview];
            }];
        }else{
            [UIView animateWithDuration:0.25f animations:^{
                [lb_temp setFrame:CGRectMake(0 - ((lb_temp.frame.size.width / 4) * 3), lb_temp.frame.origin.y, lb_temp.frame.size.width, lb_temp.frame.size.height)];
            }completion:^(BOOL finished){
                if(lb_temp.tag == tag){
                    [UIView animateWithDuration:0.15f animations:^{
                        [lb_temp setFrame:CGRectMake(0 - ((lb_temp.frame.size.width / 7) * 2), lb_temp.frame.origin.y, lb_temp.frame.size.width, lb_temp.frame.size.height)];
                        [self.view_dialog setAlpha:1];
                    }];
                }
            }];
        }
    }
    self.isStationsPullAside = !self.isStationsPullAside;
}


@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末惰瓜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子汉矿,更是在濱河造成了極大的恐慌崎坊,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件洲拇,死亡現(xiàn)場(chǎng)離奇詭異奈揍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)赋续,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門男翰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人纽乱,你說我怎么就攤上這事蛾绎。” “怎么了鸦列?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵租冠,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我薯嗤,道長(zhǎng)顽爹,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任应民,我火速辦了婚禮话原,結(jié)果婚禮上夕吻,老公的妹妹穿的比我還像新娘。我一直安慰自己繁仁,他們只是感情好涉馅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著黄虱,像睡著了一般稚矿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捻浦,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天晤揣,我揣著相機(jī)與錄音,去河邊找鬼朱灿。 笑死昧识,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的盗扒。 我是一名探鬼主播跪楞,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼侣灶!你這毒婦竟也來了甸祭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤褥影,失蹤者是張志新(化名)和其女友劉穎池户,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凡怎,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡校焦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了栅贴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片斟湃。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖檐薯,靈堂內(nèi)的尸體忽然破棺而出凝赛,到底是詐尸還是另有隱情,我是刑警寧澤坛缕,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布墓猎,位于F島的核電站,受9級(jí)特大地震影響赚楚,放射性物質(zhì)發(fā)生泄漏毙沾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一宠页、第九天 我趴在偏房一處隱蔽的房頂上張望左胞。 院中可真熱鬧寇仓,春花似錦、人聲如沸烤宙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽躺枕。三九已至服猪,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拐云,已是汗流浹背罢猪。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叉瘩,地道東北人膳帕。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像房揭,于是被迫代替她去往敵國(guó)和親备闲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)捅暴、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,105評(píng)論 4 62
  • “奸懶饞滑”不知道是誰把這四個(gè)字組在了一起咧纠,但這四個(gè)字一般能全面形容身邊的一些人蓬痒。很多職場(chǎng)攻略只提到了那些負(fù)能...
    ArleneX閱讀 12,202評(píng)論 0 1
  • 六點(diǎn)鐘下班梧奢,太陽尚未落盡,風(fēng)衣里的肌膚尚能撲捉到陽光的余溫演痒。 有點(diǎn)晚亲轨,但是春天還是來了,特意繞了...
    花小姐的茶閱讀 590評(píng)論 22 9
  • 晨光遮眼鸟顺,讓一絲清涼顯得格外清醒惦蚊,退去一些疲憊不堪;肥的油隨風(fēng)搖曳讯嫂,鍛煉成小肌肉蹦锋;樹穿插走過無數(shù)的路,絮語葉片灑滿...
    笨魚也想飛閱讀 194評(píng)論 3 2