• ios小案例:看图记字母(简单动画)


    一、项目效果:

     

     点击字母,字母有动画效果,并且发音

    二、用到的主要知识

    1.产生动画(NSTimer计时器)

    2.声音 AVFoundation.framework

     三、新建项目,主页面设计如图

     

    四、控制器实现

    1.声明代码

    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>//导入播放音频框架

    @interface HomeViewController : UIViewController
    {
        IBOutlet UIScrollView *myScrollView;
        IBOutlet UIButton *buttonA;
        IBOutlet UIButton *buttonB;
        IBOutlet UIButton *buttonC;
        IBOutlet UIButton *buttonD;
        IBOutlet UIButton *buttonE;
        IBOutlet UIButton *buttonF;
        IBOutlet UIButton *buttonG;
        IBOutlet UIButton *buttonH;
        IBOutlet UIButton *buttonI;
        IBOutlet UIButton *buttonJ;
        IBOutlet UIButton *buttonK;
        IBOutlet UIButton *buttonL;
        IBOutlet UIButton *buttonM;
        IBOutlet UIButton *buttonN;
        IBOutlet UIButton *buttonO;
        IBOutlet UIButton *buttonP;
        IBOutlet UIButton *buttonQ;
        IBOutlet UIButton *buttonR;
        IBOutlet UIButton *buttonS;
        IBOutlet UIButton *buttonT;
        IBOutlet UIButton *buttonU;
        IBOutlet UIButton *buttonV;
        IBOutlet UIButton *buttonW;
        IBOutlet UIButton *buttonX;
        IBOutlet UIButton *buttonY;
        IBOutlet UIButton *buttonZ;
        
        AVAudioPlayer *audioPlayer;
        NSTimer *animationTimer;
        int flag;//标记点到的字母
        
    }
    //字母的点击事件
    - (IBAction)touchA;
    - (IBAction)touchB;
    - (IBAction)touchC;
    - (IBAction)touchD;
    - (IBAction)touchE;
    - (IBAction)touchF;
    - (IBAction)touchG;
    - (IBAction)touchH;
    - (IBAction)touchI;
    - (IBAction)touchJ;
    - (IBAction)touchK;
    - (IBAction)touchL;
    - (IBAction)touchM;
    - (IBAction)touchN;
    - (IBAction)touchO;
    - (IBAction)touchP;
    - (IBAction)touchQ;
    - (IBAction)touchR;
    - (IBAction)touchS;
    - (IBAction)touchT;
    - (IBAction)touchU;
    - (IBAction)touchV;
    - (IBAction)touchW;
    - (IBAction)touchX;
    - (IBAction)touchY;
    - (IBAction)touchZ;

    - (void)initAudio:(NSString *)string;//初始化声音
    - (void)doTimer;//开始计时器
    @end 

     2.实现代码

      业务逻辑:点击字母时,判断audioPlayer是否为nil。如果是,就给flag赋值,它的值用来标记所点击的字母。然后执行initAudio方法(用于初始化要播放的声音文件)。由于这个操作是点击字母A的时候发生的,而声音文件名为“A”,所以将initAudio方法的参数设为“A”。

    #import"HomeViewController.h"

    @interface HomeViewController ()

    @end

    @implementation HomeViewController

    - (IBAction)touchA
    {
        if (audioPlayer == nil) {
            flag = 1;
            [self initAudio:@"A"];
        }
    }
    - (IBAction)touchB
    {
        if (audioPlayer == nil) {
            flag = 2;
            [self initAudio:@"B"];
        }
    }
    - (IBAction)touchC
    {
        if (audioPlayer == nil) {
            flag = 3;
            [self initAudio:@"C"];
        }
    }
    - (IBAction)touchD
    {
        if (audioPlayer == nil) {
            flag = 4;
            [self initAudio:@"D"];
        }
    }
    - (IBAction)touchE
    {
        if (audioPlayer == nil) {
            flag = 5;
            [self initAudio:@"E"];
        }
    }
    - (IBAction)touchF
    {
        if (audioPlayer == nil) {
            flag = 6;
            [self initAudio:@"F"];
        }
    }
    - (IBAction)touchG
    {
        if (audioPlayer == nil) {
            flag = 7;
            [self initAudio:@"G"];
        }
    }
    - (IBAction)touchH
    {
        if (audioPlayer == nil) {
            flag = 8;
            [self initAudio:@"H"];
        }
    }
    - (IBAction)touchI
    {
        if (audioPlayer == nil) {
            flag = 9;
            [self initAudio:@"I"];
        }
    }
    - (IBAction)touchJ
    {
        if (audioPlayer == nil) {
            flag = 10;
            [self initAudio:@"J"];
        }
    }
    - (IBAction)touchK
    {
        if (audioPlayer == nil) {
            flag = 11;
            [self initAudio:@"K"];
        }
    }
    - (IBAction)touchL
    {
        if (audioPlayer == nil) {
            flag = 12;
            [self initAudio:@"L"];
        }
    }
    - (IBAction)touchM
    {
        if (audioPlayer == nil) {
            flag = 13;
            [self initAudio:@"M"];
        }
    }
    - (IBAction)touchN
    {
        if (audioPlayer == nil) {
            flag = 14;
            [self initAudio:@"N"];
        }
    }
    - (IBAction)touchO
    {
        if (audioPlayer == nil) {
            flag = 15;
            [self initAudio:@"O"];
        }
    }
    - (IBAction)touchP
    {
        if (audioPlayer == nil) {
            flag = 16;
            [self initAudio:@"P"];
        }
    }
    - (IBAction)touchQ
    {
        if (audioPlayer == nil) {
            flag = 17;
            [self initAudio:@"Q"];
        }
    }
    - (IBAction)touchR
    {
        if (audioPlayer == nil) {
            flag = 18;
            [self initAudio:@"R"];
        }
    }
    - (IBAction)touchS
    {
        if (audioPlayer == nil) {
            flag = 19;
            [self initAudio:@"S"];
        }
    }
    - (IBAction)touchT
    {
        if (audioPlayer == nil) {
            flag = 20;
            [self initAudio:@"T"];
        }
    }
    - (IBAction)touchU
    {
        if (audioPlayer == nil) {
            flag = 21;
            [self initAudio:@"U"];
        }
    }
    - (IBAction)touchV
    {
        if (audioPlayer == nil) {
            flag = 22;
            [self initAudio:@"V"];
        }
    }
    - (IBAction)touchW
    {
        if (audioPlayer == nil) {
            flag = 23;
            [self initAudio:@"W"];
        }
    }
    - (IBAction)touchX
    {
        if (audioPlayer == nil) {
            flag = 24;
            [self initAudio:@"X"];
        }
    }
    - (IBAction)touchY
    {
        if (audioPlayer == nil) {
            flag = 25;
            [self initAudio:@"Y"];
        }
    }
    - (IBAction)touchZ
    {
        if (audioPlayer == nil) {
            flag = 26;
            [self initAudio:@"Z"];
        }
    }

    - (void)initAudio:(NSString *)string
    {
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:string ofType:@"wav"]];
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
        [audioPlayer play];
        [self doTimer];
        [fileURL release];
        
    }

    - (void)doTimer
    {

        //每隔0.25s就调用一次类中的doAnimation方法,用于实现字母的闪烁效果 

        animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(doAnimation) userInfo:nil repeats:YES];
    }

    - (void)doAnimation
    {
        static int n = 0;
        if (audioPlayer.playing == YES)
        {
            switch (flag) 
            {
                case 1:
                    if (n%2 == 0)
                    {
                        buttonA.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonA.highlighted = NO;
                        n--;
                    }
                    break;
                case 2:
                    if (n%2 == 0)
                    {
                        buttonB.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonB.highlighted = NO;
                        n--;
                    }
                    break;
                case 3:
                    if (n%2 == 0)
                    {
                        buttonC.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonC.highlighted = NO;
                        n--;
                    }
                    break;
                case 4:
                    if (n%2 == 0)
                    {
                        buttonD.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonD.highlighted = NO;
                        n--;
                    }
                    break;
                case 5:
                    if (n%2 == 0)
                    {
                        buttonE.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonE.highlighted = NO;
                        n--;
                    }
                    break;
                case 6:
                    if (n%2 == 0)
                    {
                        buttonF.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonF.highlighted = NO;
                        n--;
                    }
                    break;
                case 7:
                    if (n%2 == 0)
                    {
                        buttonG.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonG.highlighted = NO;
                        n--;
                    }
                    break;
                case 8:
                    if (n%2 == 0)
                    {
                        buttonH.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonH.highlighted = NO;
                        n--;
                    }
                    break;
                case 9:
                    if (n%2 == 0)
                    {
                        buttonI.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonI.highlighted = NO;
                        n--;
                    }
                    break;
                case 10:
                    if (n%2 == 0)
                    {
                        buttonJ.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonJ.highlighted = NO;
                        n--;
                    }
                    break;

                case 11:
                    if (n%2 == 0)
                    {
                        buttonK.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonK.highlighted = NO;
                        n--;
                    }
                    break;
                case 12:
                    if (n%2 == 0)
                    {
                        buttonL.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonL.highlighted = NO;
                        n--;
                    }
                    break;
                case 13:
                    if (n%2 == 0)
                    {
                        buttonM.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonM.highlighted = NO;
                        n--;
                    }
                    break;
                case 14:
                    if (n%2 == 0)
                    {
                        buttonN.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonN.highlighted = NO;
                        n--;
                    }
                    break;
                case 15:
                    if (n%2 == 0)
                    {
                        buttonO.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonO.highlighted = NO;
                        n--;
                    }
                    break;

                case 16:
                    if (n%2 == 0)
                    {
                        buttonP.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonP.highlighted = NO;
                        n--;
                    }
                    break;
                case 17:
                    if (n%2 == 0)
                    {
                        buttonQ.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonQ.highlighted = NO;
                        n--;
                    }
                    break;
                case 18:
                    if (n%2 == 0)
                    {
                        buttonR.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonR.highlighted = NO;
                        n--;
                    }
                    break;
                case 19:
                    if (n%2 == 0)
                    {
                        buttonS.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonS.highlighted = NO;
                        n--;
                    }
                    break;
                case 20:
                    if (n%2 == 0)
                    {
                        buttonT.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonT.highlighted = NO;
                        n--;
                    }
                    break;

                case 21:
                    if (n%2 == 0)
                    {
                        buttonU.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonU.highlighted = NO;
                        n--;
                    }
                    break;
                case 22:
                    if (n%2 == 0)
                    {
                        buttonV.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonV.highlighted = NO;
                        n--;
                    }
                    break;
                case 23:
                    if (n%2 == 0)
                    {
                        buttonW.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonW.highlighted = NO;
                        n--;
                    }
                    break;
                case 24:
                    if (n%2 == 0)
                    {
                        buttonX.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonX.highlighted = NO;
                        n--;
                    }
                    break;
                case 25:
                    if (n%2 == 0)
                    {
                        buttonY.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonY.highlighted = NO;
                        n--;
                    }
                    break;
                case 26:
                    if (n%2 == 0)
                    {
                        buttonZ.highlighted = YES;
                        n++;
                    }
                    else 
                    {
                        buttonZ.highlighted = NO;
                        n--;
                    }
                    break;

                default:
                    break;
            }
        }
        else
        {
            [animationTimer invalidate];
            [audioPlayer release];
            audioPlayer = nil;
            switch (flag)
            {
                case 1:
                    buttonA.highlighted = NO;
                    break;
                case 2:
                    buttonB.highlighted = NO;
                    break;
                case 3:
                    buttonC.highlighted = NO;
                    break;
                case 4:
                    buttonD.highlighted = NO;
                    break;
                case 5:
                    buttonE.highlighted = NO;
                    break;
                case 6:
                    buttonF.highlighted = NO;
                    break;
                case 7:
                    buttonG.highlighted = NO;
                    break;
                case 8:
                    buttonH.highlighted = NO;
                    break;
                case 9:
                    buttonI.highlighted = NO;
                    break;
                case 10:
                    buttonJ.highlighted = NO;
                    break;
                case 11:
                    buttonK.highlighted = NO;
                    break;
                case 12:
                    buttonL.highlighted = NO;
                    break;
                case 13:
                    buttonM.highlighted = NO;
                    break;
                case 14:
                    buttonN.highlighted = NO;
                    break;
                case 15:
                    buttonO.highlighted = NO;
                    break;
                case 16:
                    buttonP.highlighted = NO;
                    break;
                case 17:
                    buttonQ.highlighted = NO;
                    break;
                case 18:
                    buttonR.highlighted = NO;
                    break;
                case 19:
                    buttonS.highlighted = NO;
                    break;
                case 20:
                    buttonT.highlighted = NO;
                    break;
                case 21:
                    buttonU.highlighted = NO;
                    break;
                case 22:
                    buttonV.highlighted = NO;
                    break;
                case 23:
                    buttonW.highlighted = NO;
                    break;
                case 24:
                    buttonX.highlighted = NO;
                    break;
                case 25:
                    buttonY.highlighted = NO;
                    break;
                case 26:
                    buttonZ.highlighted = NO;
                    break;
                default:
                    break;
            }
        }
        
        
    }

    - (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 from its nib.
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        
    // e.g. self.myOutlet = nil;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);//保持横屏
    }

    @end

  • 相关阅读:
    Jenkins, How to Start, Stop or Restart your Instance
    Windows Batch: Remove file extension
    download web folder using wget
    Mac OS 下 控制台的代理设定
    Elasticsearch 配置IP白名单
    Elasticsearch 开启安全认证
    Redis安装手册
    laravel 使用自带的用户注册登录功能并修改登录方式
    go 安装环境
    laraveladmin 使用
  • 原文地址:https://www.cnblogs.com/hanjun/p/2770547.html
Copyright © 2020-2023  润新知