一个小小的笔顺程序,按钮太多,暂时实现了两个

来源:互联网 发布:手机编写javascript 编辑:程序博客网 时间:2024/06/09 17:32

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.返回按钮可回到上一个界面,笔顺写字,练习显示未写字体




0 0
原创粉丝点击