UIViewController切換、Notification、Delegate及繪圖練習

練習目標:http://www.youtube.com/watch?v=xiHBN2B1Vt4&list=UUPRP4bs_BNpx6XWI5Wm7O5g
老師範例:DrawSomething
我的作品:homework0810

在這個範例中,我使用了兩個viewController,去控制兩個頁面的畫面。

第一個用來顯示動畫的地方的viewController如下,
在這個頁面上除了球之外,還會有一個控制開關的鈕,當打開時代表不去管路徑如何直接讓球到圓點,
而關閉則會讓球一步步照著所繪的路線移動。
#import “ViewController.h”

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *straightSwitch;
@property (weak, nonatomic) IBOutlet UIImageView *myBall;

@end

@implementation ViewController

– (void)viewDidLoad
{
[super viewDidLoad];
}

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

/* 當切換畫面時會呼叫這個函數,可在此設定接收繪圖路徑資料的接收者為自己*/
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@”toDrawing”]){
ViewController2 * modalView = (ViewController2 *) segue.destinationViewController;
modalView.delegate = self;
}
}

/*這個函數是讓另一個ViewController去呼叫的,傳入path*/
-(void) DrawViewController:(ViewController2 *) ViewController path:(NSMutableArray *) path{
if([path count] == 0) return;
if(self.straightSwitch.on){
NSValue *value = [path objectAtIndex:[path count]-1];
[self performSelector:@selector(changePosition:) withObject:value afterDelay:0];
}else{
for (int i=0; i<[path count]; i++) { NSValue *value = [path objectAtIndex:i]; [self performSelector:@selector(changePosition:) withObject:value afterDelay:i*0.1]; } } } /*改變球的位置*/ -(void)changePosition:(NSValue *) value{ CGPoint point = [value CGPointValue]; CGRect rect = [self.myBall frame]; rect.origin.x = point.x - 25; rect.origin.y = point.y - 25; [self.myBall setFrame:rect]; } @end[/code] 第二個view則是用來讓使用者繪製路線的地方, 在這邊我又將繪圖的地方獨立出來成為另一個繪圖版 而我讓view與繪圖間用notification來傳遞資料。 [code lang="c"]#import "ViewController2.h" #import "DrawPanel.h" @interface ViewController2 () @property (weak, nonatomic) IBOutlet UIImageView *myBall; @property (weak, nonatomic) IBOutlet DrawPanel * myDrawPanel; @end @implementation ViewController2{ NSMutableArray * path; } @synthesize myDrawPanel; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //註冊監聽事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPathData:) name:@"getPathData" object:nil]; } //監聽繪圖版的繪圖事件接收器 -(void) getPathData:(NSNotification *) note{ NSDictionary *dict = [note userInfo]; path = [dict valueForKey:@"path"]; } //將繪圖資料array傳回第一個view - (IBAction)ActionBtnTouch:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ if([self.delegate respondsToSelector:@selector(DrawViewController:path:)]){ [self.delegate DrawViewController:self path:path]; } }]; } //在移除這個view時要順便將監聽事件移除 -(void)removeFromParentViewController{ [myDrawPanel removeObserver:self forKeyPath:@"price"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end[/code] 這個則是繪圖版的code [code lang="c"]#import "DrawPanel.h" @implementation DrawPanel @synthesize path; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } //每次碰觸時都清空array的值 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ path =[NSMutableArray array]; } //移動時將所移動到的位置紀錄下來 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint touchLocation = [touch locationInView:self]; NSValue *value = [NSValue valueWithCGPoint:touchLocation]; [path addObject:value]; [self setNeedsDisplay];//請view更新畫面 } //將資料傳給繪圖的view(發送notification) -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [[NSNotificationCenter defaultCenter] postNotificationName:@"getPathData" object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys: path , @"path",nil]]; } //實作繪圖方法 - (void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath(context); CGContextMoveToPoint(context, 55, 325); for(int i = 0;i<[path count];i++){ NSValue *value = [path objectAtIndex:i]; CGPoint newPoint = [value CGPointValue]; CGContextAddLineToPoint(context, newPoint.x, newPoint.y); } //CGContextClosePath(context); [[UIColor blackColor] setStroke]; CGContextDrawPath(context, kCGPathStroke); } @end[/code] 檔案下載:homework0810


17年資歷女工程師,專精於動畫、影像辨識以及即時串流程式開發。經常組織活動,邀請優秀的女性分享她們的技術專長,並在眾多場合分享自己的技術知識,也活躍於非營利組織,辦理活動來支持特殊兒及其家庭。期待用技術改變世界。