一個小小的筆順程序,按鈕太多,暫時實現了兩個

AppDelegate.m 代碼

#import "AppDelegate.h"
#import "PigViewController.h"

@interface AppDelegate ()
            

@end

@implementation AppDelegate
            

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    PigViewController *viewCotroller = [[PigViewController alloc]init];
    viewCotroller.view.backgroundColor = [UIColor clearColor];
    
    //類是用來創建對象
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewCotroller];//創建對象、初始化,添加根視圖器
    self.window.rootViewController = navController;
    navController.navigationBarHidden = YES;
    
    return YES;
}

  ViewController.h 頭文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property UITapGestureRecognizer *tap;
@property NSString *name;
@property NSString *firstNb;
@property NSString *audioName;
@property int numble;
- (void)createBgView;
//-(void)Swipe:(id)sender;
@end

第三個頁面的界面代碼

#import "ViewController.h"
#import "SecondViewController.h"
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController ()
{
    UIImageView *aniImageView;
    SystemSoundID soundID;
    UIButton *strokeBtn;
    UIButton *backBtn;
    UIButton *voiceBtn;
    UIButton *practiceBtn;
    UIImageView *lxImageView;
}

@end

@implementation ViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)createBgView
{
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 418)];
    bgImageView.image = [UIImage imageNamed:@"kanaDetailBgImg"];
    [self.view addSubview:bgImageView];
    
    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
    titleImageView.image = [UIImage imageNamed:@"titleView"];
    [self.view addSubview:titleImageView];
    
    UIImageView *footerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, 320, 49)];
    footerImageView.image = [UIImage imageNamed:@"bottomBg"];
    [self.view addSubview:footerImageView];
}

- (void)createButtons
{
    //1. 返回按鈕
    backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //設置按鈕的圖片
    [backBtn setImage:[UIImage imageNamed:@"backBtn"] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(10, 6, 70, 35);
    [backBtn addTarget:self action:@selector(didBackClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    //2. 清除按鈕
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //設置按鈕的圖片
    [clearBtn setImage:[UIImage imageNamed:@"clearBtn"] forState:UIControlStateNormal];
    clearBtn.frame = CGRectMake(240, 6, 70, 35);
    [clearBtn addTarget:self action:@selector(didClearClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clearBtn];
    
    //3. 下面的按鈕
    voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    voiceBtn.frame = CGRectMake(10, self.view.frame.size.height - 52, 67, 52);
    [voiceBtn setImage:[UIImage imageNamed:@"pronounceBtn_01"] forState:UIControlStateNormal];
    //按鈕按下去是高亮狀態
    [voiceBtn setImage:[UIImage imageNamed:@"pronounceBtn_02"] forState:UIControlStateHighlighted];
    [voiceBtn addTarget:self action:@selector(didPlayClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:voiceBtn];
    
    //4.筆迅
    strokeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    strokeBtn.frame = CGRectMake(130, self.view.frame.size.height - 52, 67, 52);
    [strokeBtn setImage:[UIImage imageNamed:@"strokeOrderBtn_01"] forState:UIControlStateNormal];
    //按鈕按下去是高亮狀態
    [strokeBtn setImage:[UIImage imageNamed:@"strokeOrderBtn_02"] forState:UIControlStateHighlighted];
    [strokeBtn addTarget:self action:@selector(didStrokeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:strokeBtn];
    //5.練習
    practiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    practiceBtn.frame = CGRectMake(240, self.view.frame.size.height - 52, 67, 52);
    [practiceBtn setImage:[UIImage imageNamed:@"practiceBtn_01"] forState:UIControlStateNormal];
    //按鈕按下去是高亮狀態
    [practiceBtn setImage:[UIImage imageNamed:@"practiceBtn_02"] forState:UIControlStateHighlighted];
    [practiceBtn addTarget:self action:@selector(didPracticeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:practiceBtn];
    
    
    
    
    
}
//動畫圖片的加載
- (void)createAnimation:(int)n name:(NSString *)nam
{
    aniImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 90, 225, 225)];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i = 1; i <= n; i++) {
        NSString *name = [NSString stringWithFormat:nam, i];
        UIImage *image = [UIImage imageNamed:name];
        [array addObject:image];
        if(i == n) {
            lxImageView.image = [UIImage imageNamed:name];//動畫完後顯示寫完的圖片
        }
    }
    aniImageView.animationImages = array;
    [self.view addSubview:aniImageView];
}

- (void)didBackClicked:(UIButton *)sender
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    //強制清除界面
    [lxImageView removeFromSuperview];
    [aniImageView removeFromSuperview];
    lxImageView = nil;
    aniImageView = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didPlayClicked:(UIButton *)sender
{
    if (lxImageView != nil) {
        AudioServicesPlaySystemSound(soundID);
        NSLog(@"%s", __PRETTY_FUNCTION__);
    }
}
- (void)didClearClicked:(UIButton *)sender
{
    [aniImageView stopAnimating];
    [lxImageView removeFromSuperview];
    lxImageView = nil;
    aniImageView = nil;
    NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)didStrokeBtnClicked:(UIButton *)sender
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    //先有沒寫的字顯示出來才能筆順
    if ((aniImageView != nil)|(lxImageView != nil)){
        if (aniImageView.isAnimating == NO) {
        [self createAnimation:self.numble name:self.name];
        aniImageView.animationDuration = 3;
        aniImageView.animationRepeatCount = 1;
        [aniImageView startAnimating];
        }
    }
}
- (void)didPracticeBtnClicked:(UIButton *)sender
{
    //lxImageView爲空時添加未寫的圖片
    if (lxImageView == nil) {
        lxImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 90, 225, 225)];
        lxImageView.image = [UIImage imageNamed:self.firstNb];
        [self.view addSubview:lxImageView];
        NSLog(@"%s", __PRETTY_FUNCTION__);
    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createBgView];
    [self createButtons];

    
    NSURL *url = [[NSBundle mainBundle] URLForResource:self.audioName withExtension:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer  alloc] init];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [swipe addTarget:self action:@selector(Swipe:)];
    [self.view addGestureRecognizer:swipe];
    
}

-(void)Swipe:(id)sender
{
    SecondViewController *viewcontroller = [[SecondViewController alloc] init];
    viewcontroller.view.backgroundColor = [UIColor blackColor];
    [self.navigationController pushViewController:viewcontroller animated:YES];
    
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

第三個頁面SecondViewController.m,顯示由ViewController.傳過來的圖片數據、播放音頻文件

#import "SecondViewController.h"
#import "ViewController.h"

@interface SecondViewController ()
{
ViewController *viewCtrl;
}
@end

@implementation SecondViewController

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

- (void)createBgView
{
    //背景圖片的顯示
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
    bgImageView.image = [UIImage imageNamed:@"titleView"];
    [self.view addSubview:bgImageView];
    
    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, self.view.frame.size.height -49)];
    titleImageView.image = [UIImage imageNamed:@"bg_01"];
    [self.view addSubview:titleImageView];
    
    UIImageView *footerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, self.view.frame.size.height -49)];
    footerImageView.image = [UIImage imageNamed:@"bg_01_01"];
    [self.view addSubview:footerImageView];
}
//封裝按鈕
- (void)createBtn:(SEL)action frame:(CGRect)frame
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

//圖片、聲音數據的封裝
- (void)data:(int)numble name:(NSString *)nm1 aodioName:(NSString *)nm2 firstNb:(NSString *)nm3
{
    viewCtrl.numble = numble;//圖片張數
    viewCtrl.name = nm1;//動畫名字
    viewCtrl.audioName = nm2;//聲音名字
    viewCtrl.firstNb = nm3;//第一張圖片的名字
    [self.navigationController pushViewController:viewCtrl animated:YES];
}

- (void)didBtn1
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [self data:17 name:@"nu%04d" aodioName:@"042_nu" firstNb:@"nu0001"];
}

- (void)didBtn2
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [self data:15 name:@"na%04d" aodioName:@"040_na" firstNb:@"na0001"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createBgView];
    //翻頁手勢,回到首頁
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] init];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [swipe addTarget:self action:@selector(Swipe:)];
    [self.view addGestureRecognizer:swipe];
    
    viewCtrl = [[ViewController alloc] init];
    
    [self createBtn:@selector(didBtn1) frame:CGRectMake(151,230, 50, 30)];//創建按鈕
    [self createBtn:@selector(didBtn2) frame:CGRectMake(49,227, 50, 30)];
}

- (void)Swipe:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.hidesBarsOnTap = YES;

}


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

@end
第一個頁面的頭文件PigViewController.h

#import <UIKit/UIKit.h>

@interface PigViewController : UIViewController
@property UITapGestureRecognizer *tap;
@end
第一個頁面顯示了一張背景圖片,點擊可以跳到第二個頁面,選擇筆畫

#import "PigViewController.h"
#import "SecondViewController.h"

@interface PigViewController ()

@end

@implementation PigViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgImageView.image = [UIImage imageNamed:@"main_Bg"];
    [self.view addSubview:bgImageView];
    //創建了一個手勢
    _tap = [[UITapGestureRecognizer alloc] init];
    [_tap addTarget:self action:@selector(Tap:)];
    [self.view addGestureRecognizer:_tap];
}

- (void)Tap:(id)sender
{
  //一個手勢,點擊可以跳到下一個頁面 
 // self.navigationController.hidesBarsOnTap = YES;
    SecondViewController *secondCtrl = [[SecondViewController alloc] init];
    UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:secondCtrl];
    self.navigationController.toolbarHidden = YES;
    navCtrl.navigationBarHidden = YES;
    //彈出一個模態視圖
    [self presentViewController:navCtrl animated:NO completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
 
1.點擊畫面跳到第二個頁面


2.實現的兩個字  nu、na 點擊可跳到筆順界面

3.返回按鈕可回到上一個界面,筆順寫字,練習顯示未寫字體




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章