斯坦福大学公开课:IOS 7应用开发 lecture10
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了斯坦福大学公开课:IOS 7应用开发 lecture10相关的知识,希望对你有一定的参考价值。
1.Now,this line of code could cause trouble.If self.image is nil,because I told you that if you have a method,this is just a getter of the image that returns a struct,and you send it to nil,you’ll get undefined results.(47:00)
2.Zomming in,really simple,remember I said you only need to do two things,okay?One is you got to set the scrollView’s minimum and maximum zoom scale.And the other thing we need to do besides setting those is to set the scrollView’s delegate and implement that other method.
3.Main queue:There is a very special queue called the “main queue”.All UI activity MUST occur on this queue and this queue only.And conversely,non-UI activity that is at all time consuming must NOT occur on that queue.We want our UI ti be responsive!Blocks are pulled off and worked on in the main queue only when it is “quiet”.
4.Executing a block on another queue:
dispatch_queue_t queue = …;
dispatch_async(queue,^{});
Getting the main queue
dispatch_queue_t mainQ = dispatch_get_main_queue(); NSOperationQueue *mainQ = [NSOperationQueue mainQueue];//for object-oriented APIs
Creating a queue(not the main queue)
dispatch_queue_t otherQ = dispatch_queue_create(“name”,NULL);//name a const char *!
-(void)performSelectorOnMainThread:(SEL)aMethod withObject:(id)obj waitUntilDone(BOOL)waitUntilDone; dispatch_async(dispatch_get_main_queue(),^{/*call a method*/});
5.Example of an ios API uses multithreading:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:@“http://….”]]; NSURLConfiguration *configuration = …; NSURLSession *session = …; NSURLSessionDownloadTask *task; task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *localfile,NSURLResponse *response,NSError *error){}]; [task resume];
downloadTaskWithRequest:completionHandler:will do its work(downloading that URL).NOT in the main thread(i.e.it will not block the UI while it waits not the network).
The completionHandler block above might execute on the main thread(or not)depending on how you create the NSURLSession.
On the main queue...
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]]; NSURLSessionDownloadTask *task; task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *localfile,NSURLResponse *response,NSError *error){ //yes,can do UI things directly because this is called on the main queue }]; [task resume];
Since the delegateQueue is the main queue,our completionHandler will be on the main queue.When the URL is done downloading,the block above will execute on the main queue.Thus we can do any UI code we want there.
Off the main queue...
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];//no delegateQueue NSURLSessionDownloadTask *task; task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile,NSURLResponse *response,NSError *error){ dispatch_async(dispatch_get_main_queue(),^{//do UI things});or
[self performSelectorOnMainThread:@selector(doUIthings)withObject:nil waitUntilDone:NO]; }]; [task resume];
In this case,you can’t do any UI stuff because the completionHandler is not on the main queue.To do UI stuff,you have to post a block(or call a method)back on the main queue.
6.How do you create a UIScrollView?
Just like any other UIViews.Drag out in a storyboard or use alloc/initWithFrame:.Or select a UIView in your storyboard and choose”Embed In -> Scroll View”from Editor menu.
Or add your “too big” UIView using addSubview:
UIImage *image = [UIImage imageNamed:@“bigimage.jpg”]; UIImageView *iv = [[UIImageView alloc]initWithImage:image];//frame.size = image.size [scrollView addSubview:iv];
All of the subviews’ frame will be in the UIScrollView’s content area’s coordinate system.(that is,(0,0) in the upper left & width and height of contentSize.width & .height).
Don’t forget to set the contentSize:common bug is to do the above 3 lines of code(or embed in Xcode)and forget to say:
scrollView.contentSize = imageView.bounds.size;
7.Zooming:All UIView’s have a property(transform)which is an affine transform(translate,scale,rotate).Scroll view simply modifies this transform when you zoom.Zooming is also going to affect the scroll view’s contentSize and contentOffset.
Will not work without minimum/maximum zoom scale being set:
scrollView.minimumZoomScale = 0.5; scrollView.maximumZoomScale = 2.0;
Will not work without delegate method to specify view to zoom
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)sender;
If your scroll view only has one subview,you return it here.More than one?Up to you.
以上是关于斯坦福大学公开课:IOS 7应用开发 lecture10的主要内容,如果未能解决你的问题,请参考以下文章
[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture4
[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture6
[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture5