2013年2月9日 星期六

NSNotificationCenter通知中心(用於一對多的通知)

//發送端:
 [NSNotificationCenter.defaultCenter postNotificationName:@"playIntro" object:self];

//接收端:
 [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playIntro) name:@"playIntro" object:self.moviePlayer];

 //接收後註銷:
 [NSNotificationCenter.defaultCenter removeObserver:self name:@"playIntro" object:self.moviePlayer];

2013年2月4日 星期一

Iterative與Recursive的差異


迭代法以確定的部分作為起始點,循序漸進推演,最後求得答案。

遞迴法找出一套縮小問題範疇的規律,以此規律不斷縮小問題,直到能釐清細節,找到確定的部份。

迭代法與遞迴法恰好顛倒:迭代法是針對已知,逐步累積,直至周全;遞迴法是針對未知,反覆拆解,直至精細。

截自http://www.csie.ntnu.edu.tw/~u91029/IterativeRecursive.html

開場影片

//最好調整成放在AppDelegate中,因為View不會那麼早被初始化

header, #import <MediaPlayer/MediaPlayer.h>
@interface RootViewController ()
{  
}
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
 
@implementation RootViewController
@synthesize moviePlayer;
 
-(void)playIntro{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"appIntro" ofType:@"mp4"]];
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    //註冊通知中心,當影片播放完畢,進行回呼方法(playbackStateDidFinish)
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateDidFinish)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayer ];
    //播放器參數設定
    self.moviePlayer.controlStyle = MPMovieControlStyleNone;
    self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
    self.moviePlayer.repeatMode = MPMovieRepeatModeNone;
    self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin
   |UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
    //設定播放器大小
    [self.moviePlayer.view setFrame:self.view.bounds];
    [self.view addSubview:self.moviePlayer.view];
    //播放
    [self.moviePlayer play];
}
 
- (void)playbackStateDidFinish{
    //註銷通知中心
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayer];
    //停止播放
    [self.moviePlayer stop];
    //移除播放器
    [self.moviePlayer.view removeFromSuperview];
}

UIWebView截取html

//載取html改變頁面寬度後塞回
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//修改頁面meta值
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", webView.frame.size.width];
[webView stringByEvaluatingJavaScriptFromString:meta];
}

//增加UTF-8編碼
     [webView stringByEvaluatingJavaScriptFromString:  @"var tagHead =document.documentElement.firstChild;" 
    "var tagMeta = document.createElement(\"meta\");"
    "tagMeta.setAttribute(\"http-equiv\", \"Content-Type\");"   "tagMeta.setAttribute(\"content\", \"text/html; charset=utf-8\");"
    "var tagHeadAdd = tagHead.appendChild(tagMeta);"]; 
//截取網頁圖片,修改圖片大小
[webView stringByEvaluatingJavaScriptFromString:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=380;" //頁面寬度
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"myimg.height = myimg.height * (maxwidth/oldwidth);"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"];
 
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];  

2013年2月3日 星期日

旋轉手勢辨識


#import <UIKit/UIKit.h>

@interface MyImageView : UIImageView{
    CGPoint startPoint;
    CGAffineTransform currentTransform;
}

@end


#import "MyImageView.h"
#import <QuartzCore/QuartzCore.h>
@implementation MyImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder* )aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        //啟動觸控
        self.userInteractionEnabled= YES;
        //設定圖檔
        self.image = [UIImage imageNamed:@"image4.jpg"];
        //加入手勢
        
        /*
        UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(doMove:)];
        [self addGestureRecognizer:pan];
        */
        /*
        UISwipeGestureRecognizer* swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doLeft:)];
        swipe1.direction = UISwipeGestureRecognizerDirectionLeft;
        //要求雙擊
        swipe1.numberOfTouchesRequired = 2;
        [self addGestureRecognizer:swipe1];
        
        UISwipeGestureRecognizer* swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doRight:)];
        //要求雙擊
        swipe2.numberOfTouchesRequired = 2;
        swipe2.direction = UISwipeGestureRecognizerDirectionRight;
        [self addGestureRecognizer:swipe2];
        */
        
        UIRotationGestureRecognizer* rotation= [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doRotate:)];
        [self addGestureRecognizer:rotation];
        
    }
    return self;
}

-(void)doRotate:(UIRotationGestureRecognizer*)recognizer{
    //recognizer中取得並旅轉
    self.layer.affineTransform = CGAffineTransformRotate(currentTransform, recognizer.rotation);
}

-(void)doLeft:(UISwipeGestureRecognizer*)recognizer{
    CGPoint newPoint = CGPointMake(self.center.x-50, self.center.y);
    self.center = newPoint;
}
-(void)doRight:(UISwipeGestureRecognizer*)recognizer{
    CGPoint newPoint = CGPointMake(self.center.x+50, self.center.y);
    self.center = newPoint;
}
/*
-(void)doMove:(UIPanGestureRecognizer*)recognizer{
    //取得解控點
    CGPoint clickPoint = [recognizer translationInView:self];
    NSLog(@"Move的點是(%.1f,%.1f)",clickPoint.x,clickPoint.y);
    //開始移動
    CGPoint newPoint = CGPointMake(startPoint.x+clickPoint.x, startPoint.y+clickPoint.y);
    self.center = newPoint;
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //取得觸控點
    CGPoint clickPoint = [[touches anyObject]locationInView:self];
    NSLog(@"觸控點在(%.1f,%.1f)",clickPoint.x,clickPoint.y);
    startPoint = clickPoint;
    currentTransform=self.layer.affineTransform;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

滑動手勢辨識


#import <UIKit/UIKit.h>

@interface MyImageView : UIImageView{
    CGPoint startPoint;
}

@end

#import "MyImageView.h"

@implementation MyImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder* )aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        //啟動觸控
        self.userInteractionEnabled= YES;
        //設定圖檔
        self.image = [UIImage imageNamed:@"image4.jpg"];
        //加入手勢
        
        /*
        UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(doMove:)];
        [self addGestureRecognizer:pan];
        */
        UISwipeGestureRecognizer* swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doLeft:)];
        swipe1.direction = UISwipeGestureRecognizerDirectionLeft;
        //要求雙擊
        swipe1.numberOfTouchesRequired = 2;
        [self addGestureRecognizer:swipe1];
        
        UISwipeGestureRecognizer* swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doRight:)];
        //要求雙擊
        swipe2.numberOfTouchesRequired = 2;
        swipe2.direction = UISwipeGestureRecognizerDirectionRight;
        [self addGestureRecognizer:swipe2];
        
    }
    return self;
}

-(void)doLeft:(UISwipeGestureRecognizer*)recognizer{
    CGPoint newPoint = CGPointMake(self.center.x-50, self.center.y);
    self.center = newPoint;
}
-(void)doRight:(UISwipeGestureRecognizer*)recognizer{
    CGPoint newPoint = CGPointMake(self.center.x+50, self.center.y);
    self.center = newPoint;
}
/*
-(void)doMove:(UIPanGestureRecognizer*)recognizer{
    //取得解控點
    CGPoint clickPoint = [recognizer translationInView:self];
    NSLog(@"Move的點是(%.1f,%.1f)",clickPoint.x,clickPoint.y);
    //開始移動
    CGPoint newPoint = CGPointMake(startPoint.x+clickPoint.x, startPoint.y+clickPoint.y);
    self.center = newPoint;
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //取得觸控點
    CGPoint clickPoint = [[touches anyObject]locationInView:self];
    NSLog(@"觸控點在(%.1f,%.1f)",clickPoint.x,clickPoint.y);
    startPoint = clickPoint;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end


位移手勢辨識


#import <UIKit/UIKit.h>

@interface MyImageView : UIImageView{
    CGPoint startPoint;
}

@end


#import "MyImageView.h"

@implementation MyImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder* )aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        //啟動觸控
        self.userInteractionEnabled= YES;
        //設定圖檔
        self.image = [UIImage imageNamed:@"image4.jpg"];
        //加入手勢
        UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(doMove:)];
        [self addGestureRecognizer:pan];
    }
    return self;
}

-(void)doMove:(UIPanGestureRecognizer*)recognizer{
    //取得解控點
    CGPoint clickPoint = [recognizer translationInView:self];
    NSLog(@"Move的點是(%.1f,%.1f)",clickPoint.x,clickPoint.y);
    //開始移動
    CGPoint newPoint = CGPointMake(startPoint.x+clickPoint.x, startPoint.y+clickPoint.y);
    self.center = newPoint;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //取得觸控點
    CGPoint clickPoint = [[touches anyObject]locationInView:self];
    NSLog(@"觸控點在(%.1f,%.1f)",clickPoint.x,clickPoint.y);
    startPoint = clickPoint;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end