【代碼筆記】點擊一個按鈕會出現多個按鈕的動畫效果

来源:http://www.cnblogs.com/yang-guang-girl/archive/2016/01/15/5132321.html
-Advertisement-
Play Games

一,效果圖。二,工程圖。三,代碼。RootViewController.h#import @interface RootViewController : UIViewController{ UIImageView *iCanImageView; UIImageView *menu_car...


一,效果圖。

二,工程圖。

三,代碼。

RootViewController.h

複製代碼
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
{
    UIImageView *iCanImageView;
    UIImageView *menu_carImageView;
    UIImageView *menu_movieImageView;
    UIImageView *menu_setImageView;
    UIImageView *menu_photoImageView;
    BOOL isRonating;
    int count;
}
@end
複製代碼

 

RootViewController.m

複製代碼
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化背景圖
    [self initBackgroundView];
    
    
}
#pragma -mark -functions
-(void)initBackgroundView
{
    //隱藏導航條
    self.navigationController.navigationBarHidden=YES;
    
    //設置背景圖片
    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"main_bg.png"]];
    bgImage.frame = self.view.bounds;
    
    //背景圖添加手勢
    UITapGestureRecognizer *bgTgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgClick)];
    [bgImage addGestureRecognizer:bgTgr];
    bgImage.userInteractionEnabled = YES;
    
    [self.view addSubview:bgImage];
    
    
    //背景圖上的小圖標
    iCanImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ican.png"]];
    iCanImageView.center = CGPointMake(50, 400);
    
    //小圖標添加手勢
    UITapGestureRecognizer *iCanTgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iCanClick)];
    [iCanImageView addGestureRecognizer:iCanTgr];
    iCanImageView.userInteractionEnabled = YES;
    
    
    //彈出的4個設置的小圖標
    //車圖標
    menu_carImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_car.png"]];
    menu_carImageView.tag = 3;
    menu_carImageView.center = iCanImageView.center;
    
    //視頻的圖標
    menu_movieImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_movie.png"]];
    menu_movieImageView.tag = 4;
    menu_movieImageView.center = iCanImageView.center;
    
    //圖片的圖標
    menu_photoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
    menu_photoImageView.tag = 5;
    menu_photoImageView.center = iCanImageView.center;
    
    //設置的圖標
    menu_setImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_set.png"]];
    menu_setImageView.tag = 6;
    menu_setImageView.center = iCanImageView.center;
    
    
    [self.view addSubview:menu_carImageView];
    [self.view addSubview:menu_movieImageView];
    [self.view addSubview:menu_photoImageView];
    [self.view addSubview:menu_setImageView];
    [self.view addSubview:iCanImageView];
    
    
    // 將小圖片都添加到數組,最後迴圈數組為每一個小圖片添加點擊手勢
    NSArray *imageViewArr = [[NSArray alloc] initWithObjects:menu_carImageView,menu_movieImageView,menu_photoImageView,menu_setImageView, nil];
    
    for(UIImageView *view in imageViewArr)
    {
        UITapGestureRecognizer *jumpTo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Jump:)];
        view.userInteractionEnabled = YES;
        [view addGestureRecognizer:jumpTo];
    }
    
    // 判斷圖標是否旋轉,還有小圖標是否飛出
    isRonating = NO;
    
    // 用於計數小圖片旋轉的時間
    count = 0;
}
#pragma -mark -doClickActions
//點擊iCan圖標,彈出圖標
-(void)iCanClick
{
    CGAffineTransform trans = iCanImageView.transform;
    if(isRonating == NO)
    {
        CGAffineTransform newTrans = CGAffineTransformRotate(trans, -2*M_1_PI);
        [UIView animateWithDuration:0.3 animations:^{
            iCanImageView.transform = newTrans;
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(GoOut:) userInfo:nil repeats:YES];
        }];
        isRonating = YES;
    }
    else
    {
        CGAffineTransform newTrans = CGAffineTransformRotate(trans, 2*M_1_PI);
        [UIView animateWithDuration:0.3 animations:^{
            iCanImageView.transform = newTrans;
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(Back:) userInfo:nil repeats:YES];
        }];
        isRonating = NO;
    }
}
//點擊背景圖,圖標旋轉回原位
-(void)bgClick
{
    if(isRonating == YES)
    {
        CGAffineTransform trans = iCanImageView.transform;
        CGAffineTransform newTrans = CGAffineTransformRotate(trans, 2*M_1_PI);
        [UIView animateWithDuration:0.3 animations:^{
            iCanImageView.transform = newTrans;
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(Back:) userInfo:nil repeats:YES];
        }];
        isRonating = NO;
    }
}
//彈出的動作
-(void)GoOut:(id)sender
{
    NSTimer *tiemr = (NSTimer *)sender;
    count++;
    [UIView animateWithDuration:0.2 animations:^{
        menu_carImageView.center = [self location:CGPointMake(-10, 128)];
    }];
    if(count>2)
        [UIView animateWithDuration:0.2 animations:^{
            menu_movieImageView.center = [self location:CGPointMake(45, 100)];
        }];
    if(count>3)
        [UIView animateWithDuration:0.2 animations:^{
            menu_photoImageView.center = [self location:CGPointMake(88, 55)];
        }];
    if(count>4)
        [UIView animateWithDuration:0.2 animations:^{
            menu_setImageView.center = [self location:CGPointMake(105, -8)];
        }];
    if(count>5)
    {
        count = 0;
        [tiemr invalidate];
    }
}
//回歸原位的動作
-(void)Back:(id)sender;
{
    NSTimer *timer = (NSTimer *)sender;
    count ++;
    [UIView animateWithDuration:0.2 animations:^{
        [self Ronate:menu_setImageView];
    }];
    if(count>3)
        [UIView animateWithDuration:0.2 animations:^{
            [self Ronate:menu_photoImageView];
        }];
    if(count>6)
        [UIView animateWithDuration:0.2 animations:^{
            menu_setImageView.center = iCanImageView.center;
        }];
    if(count>8)
        [UIView animateWithDuration:0.2 animations:^{
            menu_photoImageView.center = iCanImageView.center;
        }];
    if(count>5)
        [UIView animateWithDuration:0.2 animations:^{
            [self Ronate:menu_movieImageView];
        }];
    if(count>9)
        [UIView animateWithDuration:0.2 animations:^{
            menu_movieImageView.center = iCanImageView.center;
        }];
    if(count >7)
        [UIView animateWithDuration:0.2 animations:^{
            [self Ronate:menu_carImageView];
        }];
    if(count>10)
        [UIView animateWithDuration:0.2 animations:^{
            menu_carImageView.center = iCanImageView.center;
        }];
    if(count>11)
    {
        menu_carImageView.transform = CGAffineTransformMakeRotation(0);
        menu_movieImageView.transform = CGAffineTransformMakeRotation(0);
        menu_photoImageView.transform = CGAffineTransformMakeRotation(0);
        menu_setImageView.transform = CGAffineTransformMakeRotation(0);
        count = 0;
        [timer invalidate];
    }
}
-(CGPoint)location:(CGPoint)p
{
    CGFloat x = CGRectGetMaxX(iCanImageView.frame);
    CGFloat y = iCanImageView.center.y;
    CGPoint pp = CGPointMake(x+p.x+20, y-p.y-10);
    return pp;
}
-(void)Ronate:(UIImageView *)view
{
    view.transform = CGAffineTransformMakeRotation(360.0f*count);
}
// 界面跳轉
-(void)Jump:(id)sender
{
    UIGestureRecognizer *t = (UIGestureRecognizer *)sender;
    UIImageView *view = (UIImageView *)t.view;
    UIEdgeInsets set;
    set.top = 5.0f;
    set.bottom = 5.0f;
    set.left = 5.0f;
    set.right = 5.0f;
    if(view.tag == 3)
    {
         [self Scale:view];
     }
    else if(view.tag == 4)
    {
        [self Scale:view];
     }
    else if(view.tag == 5)
    {
        [self Scale:view];
    }
    else
    {
        [self Scale:view];
    }
}

// 放大和縮小圖片
-(void)Scale:(UIImageView *)view
{
    if(view.tag == 3)
    {
        CGFloat scale = 1.5;
        CGAffineTransform trans = view.transform;
        [UIImageView animateWithDuration:0.5 animations:^{
            CGAffineTransform newTrans = CGAffineTransformScale(trans, scale, scale);
            CGAffineTransform newTrans1 = CGAffineTransformScale(trans, 0.5, 0.5);
            view.transform = newTrans;
            menu_movieImageView.transform = newTrans1;
            menu_photoImageView.transform = newTrans1;
            menu_setImageView.transform = newTrans1;
            view.alpha = 0.1;
            menu_movieImageView.alpha = 0.1;
            menu_photoImageView.alpha = 0.1;
            menu_setImageView.alpha = 0.1;
        } completion:^(BOOL finished) {
            NSLog(@"--跳轉到第一個圖標的頁面---");
        }];
    }
    else if(view.tag == 4)
    {
        CGFloat scale = 1.5;
        CGAffineTransform trans = view.transform;
        [UIImageView animateWithDuration:0.5 animations:^{
            CGAffineTransform newTrans = CGAffineTransformScale(trans, scale, scale);
            CGAffineTransform newTrans1 = CGAffineTransformScale(trans, 0.5, 0.5);
            view.transform = newTrans;
            menu_carImageView.transform = newTrans1;
            menu_photoImageView.transform = newTrans1;
            menu_setImageView.transform = newTrans1;
            view.alpha = 0.1;
            menu_carImageView.alpha = 0.1;
            menu_photoImageView.alpha = 0.1;
            menu_setImageView.alpha = 0.1;
        } completion:^(BOOL finished) {
            NSLog(@"--跳轉到第二個圖標的頁面---");

        }];
    }
    else if(view.tag == 5)
    {
        CGFloat scale = 1.5;
        CGAffineTransform trans = view.transform;
        [UIImageView animateWithDuration:0.5 animations:^{
            CGAffineTransform newTrans = CGAffineTransformScale(trans, scale, scale);
            CGAffineTransform newTrans1 = CGAffineTransformScale(trans, 0.5, 0.5);
            view.transform = newTrans;
            menu_movieImageView.transform = newTrans1;
            menu_carImageView.transform = newTrans1;
            menu_setImageView.transform = newTrans1;
            view.alpha = 0.1;
            menu_movieImageView.alpha = 0.1;
            menu_carImageView.alpha = 0.1;
            menu_setImageView.alpha = 0.1;
        } completion:^(BOOL finished) {
            NSLog(@"--跳轉到第三個圖標的頁面---");

        }];
    }
    else
    {
        CGFloat scale = 1.5;
        CGAffineTransform trans = view.transform;
        [UIImageView animateWithDuration:0.5 animations:^{
            CGAffineTransform newTrans = CGAffineTransformScale(trans, scale, scale);
            CGAffineTransform newTrans1 = CGAffineTransformScale(trans, 0.5, 0.5);
            view.transform = newTrans;
            menu_movieImageView.transform = newTrans1;
            menu_photoImageView.transform = newTrans1;
            menu_carImageView.transform = newTrans1;
            view.alpha = 0.1;
            menu_movieImageView.alpha = 0.1;
            menu_photoImageView.alpha = 0.1;
            menu_carImageView.alpha = 0.1;
        } completion:^(BOOL finished) {
            NSLog(@"--跳轉到第四個圖標的頁面---");

        }];
        
    }
}


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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • KVC的使用通常情況下,我們都是通過屬性或者定義存取方法來對實例變數進行存取,但是除此之外,我們也可以通過Key-Value-Coding(KVC)鍵值編碼來存取的實例變數的值。使用KVC過程:首先,我們定義一個Person類,代碼如下:/**Person.h*/#import @interface...
  • 在info.plist文件中添加下麵是後臺播放音頻的完整測試代碼:引入文件//後臺播放音頻設置 AVAudioSession *session = [AVAudioSession sharedInstance]; [session setActive:YES error:nil]; [sess...
  • 用linearlayout完成這樣的佈局效果,這樣的佈局還是比較常用的,具體的xml代碼如下: ...
  • hybrid app Hybrid App(混合模式移動應用)是指介於web-app、native-app這兩者之間的app,兼具“Native App良好用戶交互體驗的優勢”和“Web App跨平臺開發的優勢”。簡介編輯“雲”時代的來臨正在改變App和運營團隊之間的關係,一場不能避免的變革正在進....
  • 從當前activity中獲取et 表單中的值,並跳轉到myactivity.java所綁定的xml佈局文件上。private EditText et;Intent intent=new Intent(MainActivity.this,myactivity.class); String str...
  • 首頁在AndroidManifest.xml中添加訪問資料庫許可權 在src中新建一個連接webservice的類,名字隨意,這裡叫做“HttpConnSoap”。基本上這個類是固定的,要改的大多數就是webservice埠地址,具體代碼如下:package com.example.hospi...
  • 如果想直接查看源碼的話可以從我的Github上下載查看:https://github.com/zhanghuijun0/demo-for-android/tree/master/SlidingMenuSlidingMenu 是github上Android開源項目 能用於快速集成 Android 側滑...
  • 【版權所有,轉載請註明出處。】項目jni的關鍵代碼(項目地址見文章底部),獲取當前程式的包名com.example.shelldemo和com.example.nocrack對比,正常運行結果是this app is illegal,編譯jni我就不介紹了,項目中也帶了編譯好的so1.工具介紹IDA...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...