这是http://www.raywenderlich.com/store/ios-apprentice上的免费ios开发教程,原版已经更新到swift。我参阅的是https://github.com/eseedo/kidscoding上王寒老师的翻阅版本,是objectiv-c也是较早前的版本。这个系列的教程开发难度非常低,可以对app开发有个总体上的认识,快速上手。
以下是code方便将来翻阅:
//
// ViewController.h
// CrazyDrag
//
// Created by fengsser on 15/6/20.
// Copyright (c) 2015年 fengsser. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIAlertViewDelegate>
@end
//
// ViewController.m
// CrazyDrag
//
// Created by fengsser on 15/6/20.
// Copyright (c) 2015年 fengsser. All rights reserved.
//
#import "ViewController.h"
#import "AboutViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController (){
int currentValue;
int targetValue;
int score;
int round;
}
- (IBAction)sliderMoved:(id)sender;
- (IBAction)showAlert:(id)sender;
- (IBAction)startOver:(id)sender;
- (IBAction)showInfo:(id)sender;
@property (strong, nonatomic) IBOutlet UISlider *slider;
@property (strong, nonatomic) IBOutlet UILabel *targetLabel;
@property (strong, nonatomic) IBOutlet UILabel *scoreLabel;
@property (strong, nonatomic) IBOutlet UILabel *roundLabel;
@property(nonatomic,strong)AVAudioPlayer *audioPlayer;
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
@end
@implementation ViewController
@synthesize slider;
@synthesize targetLabel;
@synthesize audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *thumbImageNormal = [UIImage imageNamed:@"SliderThumb-Normal"];
[self.slider setThumbImage:thumbImageNormal forState:UIControlStateNormal];
UIImage *thumbImageHighlighted = [UIImage imageNamed:@"SliderThumb- Highlighted"];
[self.slider setThumbImage:thumbImageHighlighted forState:UIControlStateHighlighted];
UIImage *trackLeftImage = [[UIImage imageNamed:@"SliderTrackLeft"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];
[self.slider setMinimumTrackImage:trackLeftImage forState:UIControlStateNormal];
UIImage *trackRightImage = [[UIImage imageNamed:@"SliderTrackRight"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];
[self.slider setMaximumTrackImage:trackRightImage forState:UIControlStateNormal];
[self startRound];
[self updateLabels];
[self playBackgroundMusic];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
[self startRound];
[self updateLabels];
}
- (void)updateLabels {
self.targetLabel.text = [NSString stringWithFormat:@"%d",targetValue];
self.scoreLabel.text = [NSString stringWithFormat:@"%d",score];
self.roundLabel.text = [NSString stringWithFormat:@"%d",round];
}
- (void)startRound {
currentValue = 50;
self.slider.value = currentValue;
targetValue = arc4random()%100+1;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sliderMoved:(UISlider*)sender {
currentValue = (int)lroundf(sender.value);
}
- (IBAction)showAlert:(id)sender {
int diffent = abs(currentValue - targetValue);
int point = 100 - diffent;
round += 1;
NSString *title;
if(diffent == 0){
title = @"完美达成!";
point += 100;
}
else if(diffent < 10){
title = @"几乎完美!";
point += 50;
}
else{
title = @"差得有点远哟!";
}
score += point;
NSString *message = [NSString stringWithFormat:@"恭喜!您的得分是:%d",point];
[[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"朕知道了" otherButtonTitles:nil, nil]show];
}
- (void)startNewGame{
score = 0;
round = 0;
[self startRound];
}
- (IBAction)startOver:(id)sender {
CATransition *transition = [CATransition animation];
transition.type = kCATransitionFade; transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut];
[self startNewGame];
[self updateLabels];
[self.view.layer addAnimation:transition forKey:nil];
}
- (IBAction)showInfo:(id)sender {
AboutViewController *controller = [[AboutViewController alloc]initWithNibName:@"AboutViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
- (void)playBackgroundMusic{
NSString *musicPath = [[NSBundle mainBundle]pathForResource:@"no" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:musicPath]; NSError *error;
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if(audioPlayer ==nil){
NSString *errorInfo = [NSString stringWithString:[error description]];
NSLog(@"the error is:%@",errorInfo);
}else{
[audioPlayer play];
}
}
@end
//
// AboutViewController.h
// CrazyDrag
//
// Created by fengsser on 15/6/22.
// Copyright (c) 2015年 fengsser. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AboutViewController : UIViewController
@end
//
// AboutViewController.m
// CrazyDrag
//
// Created by fengsser on 15/6/22.
// Copyright (c) 2015年 fengsser. All rights reserved.
//
#import "AboutViewController.h"
@interface AboutViewController ()
- (IBAction)close:(id)sender;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation AboutViewController
@synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *htmlFile =[[NSBundle mainBundle]pathForResource:@"CrazyDrag" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]; [self.webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL];
}
- (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.
}
*/
- (IBAction)close:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
@end