lecture 10 slides

Upload: okandas

Post on 04-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Lecture 10 Slides

    1/44

    Stanford CS193pDeveloping Applications for iOS

    Fall 2013-14

  • 8/13/2019 Lecture 10 Slides

    2/44

    TodayMultithreading

    Posting blocks on queues (which are then executed on other threads).UIScrollViewA window on an arbitrarily large content area that can be moved and zoomed

    DemoImaginariumUITableView(Time Permitting)Data source-driven vertical list of views.

  • 8/13/2019 Lecture 10 Slides

    3/44

    MultithreadingQueues

    Multithreading is mostly about queues in iOS.Blocks are lined up in a queue (method calls can also be enqueued).Then those blocks are pulled off the queue and executed on an associated thre

    Main QueueThere 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 o

    We want our UI to be responsive!Blocks are pulled off and worked on in the main queue only when it is quiet.

    Other QueuesMostly iOS will create these for us as needed.Well give a quick overview of how to create your own (but usually not necessa

  • 8/13/2019 Lecture 10 Slides

    4/44

    MultithreadingExecuting a block on another queuedispatch_queue_t queue = ;

    dispatch_async(queue, ^{ });

    Getting the main queuedispatch_queue_t mainQ = dispatch_get_main_queue();

    NSOperationQueue *mainQ = [NSOperationQueue mainQueue]; // for object-or

    Creating a queue (not the main queue)dispatch_queue_t otherQ = dispatch_queue_create(name, NULL);

    //name

    aEasy mode invoking a method on the main queueNSObjectmethod - (void)performSelectorOnMainThread:(SEL)aMethod

    withObject:(id)obj

    waitUntilDone:(BOOL)waitUntilDone;

    dispatch_async(dispatch_get_main_queue(), ^{ /* call aMethod*/});

  • 8/13/2019 Lecture 10 Slides

    5/44

    MultithreadingExample of an iOS API that uses multithreadingNSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:@http://..

    NSURLConfiguration *configuration = ;

    NSURLSession *session = ;

    NSURLSessionDownloadTask *task;

    task = [session downloadTaskWithRequest:request

    completionHandler:^(NSURL *localfile, NSURLResponse *response,

    /* want to do UI things here, can I?*/}];

    [task resume];

    downloadTaskWithRequest:completionHandler:will do its work (downloading t

    NOT in the main thread (i.e. it will not block the UI while it waits on the net

    The completionHandlerblock above might execute on the main thread (or not)

    depending on how you create the NSURLSession.Lets look at the two options (on or off the main queue)

  • 8/13/2019 Lecture 10 Slides

    6/44

    MultithreadingOn the main queue NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration

    delegate:nil

    delegateQueue:[NSOperationQueue main

    NSURLSessionDownloadTask *task;

    task = [session downloadTaskWithRequest:request

    completionHandler:^(NSURL *localfile, NSURLResponse *response,

    /* yes, can do UI things directly because this is called on the main queue*/}];

    [task resume];

    Since the delegateQueueis the main queue, our completionHandlerwill be on

    When the URL is done downloading, the block above will execute on the main q

    Thus we can do any UI code we want there.Of course, if you are doing non-UI things here, theyd best be quick (dont block

  • 8/13/2019 Lecture 10 Slides

    7/44

    MultithreadingOff the main queue NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];// no

    NSURLSessionDownloadTask *task;

    task = [session downloadTaskWithRequest:request

    completionHandler:^(NSURL *localfile, NSURLResponse *response,

    dispatch_async(dispatch_get_main_queue(), ^{ /* do UI things */ });

    or [self performSelectorOnMainThread:@selector(doUIthings) withObject:nil waitUnt

    }];

    [task resume];

    In this case, you cant do any UI stuff because the completionHandler is not on

    To do UI stuff, you have to post a block (or call a method) back on the main que

  • 8/13/2019 Lecture 10 Slides

    8/44

    UIScrollView

  • 8/13/2019 Lecture 10 Slides

    9/44

    Adding subviews to a normal UIView ...subview.frame = ...;[view addSubview:subview];

  • 8/13/2019 Lecture 10 Slides

    10/44

    Adding subviews to a normal UIView ...subview.frame = ...;[view addSubview:subview];

  • 8/13/2019 Lecture 10 Slides

    11/44

    Adding subviews to a normal UIView ...subview.frame = ...;[view addSubview:subview];

  • 8/13/2019 Lecture 10 Slides

    12/44

    Adding subviews to a UIScrollView ...

  • 8/13/2019 Lecture 10 Slides

    13/44

    Adding subviews to a UIScrollView ...scrollView.contentSize = CGSizeMake(3000, 2000);

  • 8/13/2019 Lecture 10 Slides

    14/44

  • 8/13/2019 Lecture 10 Slides

    15/44

    Adding subviews to a UIScrollView ...

    subview2.frame = CGRectMake(50, 100, 2500, 1600);scrollView.contentSize = CGSizeMake(3000, 2000);

    [view addSubview:subview2];

  • 8/13/2019 Lecture 10 Slides

    16/44

    subview2.frame = CGRectMake(50, 100, 2500, 1600);[view addSubview:subview];

    scrollView.contentSize = CGSizeMake(3000, 2000);

    Adding subviews to a UIScrollView...

  • 8/13/2019 Lecture 10 Slides

    17/44

    subview2.frame = CGRectMake(50, 100, 2500, 1600);[view addSubview:subview];

    scrollView.contentSize = CGSizeMake(3000, 2000);

    Adding subviews to a UIScrollView...

  • 8/13/2019 Lecture 10 Slides

    18/44

    subview2.frame = CGRectMake(50, 100, 2500, 1600);[view addSubview:subview];

    scrollView.contentSize = CGSizeMake(3000, 2000);

    Adding subviews to a UIScrollView...

  • 8/13/2019 Lecture 10 Slides

    19/44

    subview2.frame = CGRectMake(50, 100, 2500, 1600);[view addSubview:subview];

    scrollView.contentSize = CGSizeMake(3000, 2000);

    Adding subviews to a UIScrollView...

  • 8/13/2019 Lecture 10 Slides

    20/44

    subview2.frame = CGRectMake(50, 100, 2500, 1600);[view addSubview:subview];

    scrollView.contentSize = CGSizeMake(3000, 2000);

    Adding subviews to a UIScrollView...

  • 8/13/2019 Lecture 10 Slides

    21/44

    Positioning subviews in a UIScrollView...

  • 8/13/2019 Lecture 10 Slides

    22/44

    Positioning subviews in a UIScrollView...subview1.frame = CGRectMake(2250, 50, 120, 180);

  • 8/13/2019 Lecture 10 Slides

    23/44

    subview2.frame = CGRectMake(0, 0, 2500, 1600);

    Positioning subviews in a UIScrollView...

    P iti i b i i

  • 8/13/2019 Lecture 10 Slides

    24/44

    subview2.frame = CGRectMake(0, 0, 2500, 1600);scrollView.contentSize = CGSizeMake(2500, 1600);

    Positioning subviews in a UIScrollView...

    V il!

  • 8/13/2019 Lecture 10 Slides

    25/44

    subview2.frame = CGRectMake(0, 0, 2500, 1600);scrollView.contentSize = CGSizeMake(2500, 1600);

    Voil!

    V il!

  • 8/13/2019 Lecture 10 Slides

    26/44

    subview2.frame = CGRectMake(0, 0, 2500, 1600);scrollView.contentSize = CGSizeMake(2500, 1600);

    Voil!

    V il!

  • 8/13/2019 Lecture 10 Slides

    27/44

    subview2.frame = CGRectMake(0, 0, 2500, 1600);scrollView.contentSize = CGSizeMake(2500, 1600);

    Voil!

    Voil!

  • 8/13/2019 Lecture 10 Slides

    28/44

    subview2.frame = CGRectMake(0, 0, 2500, 1600);scrollView.contentSize = CGSizeMake(2500, 1600);

    Voil!

  • 8/13/2019 Lecture 10 Slides

    29/44

    Visible area of a scroll view

  • 8/13/2019 Lecture 10 Slides

    30/44

    Visible area of a scroll viewscrollView.bounds

    Visible area of a scroll views subview in that views coord

  • 8/13/2019 Lecture 10 Slides

    31/44

    CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:subview]

    Visible area of a scroll viewscrollView.bounds

    s subview in that views coord

    Whats the difference? Might be scaled (due to zooming), for example.

    UIS llVi

  • 8/13/2019 Lecture 10 Slides

    32/44

    UIScrollViewHow do you create one?Just like any other UIView. Drag out in a storyboard or use alloc/initWithFr

    Or select a UIViewin your storyboard and choose Embed In -> Scroll View frOr add your too big UIViewusing addSubview:UIImage *image = [UIImage imageNamed:@bigimage.jpg];

    UIImageView *iv = [[UIImageView alloc] initWithImage:image];// frame.

    [scrollView addSubview:iv];

    Add more subviews if you want.All of the subviews frames will be in the UIScrollViews content areas coordin

    (that is, (0,0) in the upper left & width and height of contentSize.width& .

    Dont forget to set the contentSizeCommon bug is to do the above 3 lines of code (or embed in Xcode) and forget

    scrollView.contentSize = imageView.bounds.size

    UIS llVi

  • 8/13/2019 Lecture 10 Slides

    33/44

    UIScrollViewScrolling programmatically- (void)scrollRectToVisible:(CGRect)aRect animated:(BOOL)animated;

    Other things you can control in a scroll viewWhether scrolling is enabled.Locking scroll direction to users first move.The style of the scroll indicators (call flashScrollIndicatorswhen your scrol

    Whether the actual content is inset from the content area (contentInsetpro

    UIS llVi

  • 8/13/2019 Lecture 10 Slides

    34/44

    UIScrollViewZooming

    All UIViews have a property (transform) which is an affine transform (translate

    Scroll view simply modifies this transform when you zoom.

    Zooming is also going to affect the scroll views contentSizeand contentOffs

    Will not work without minimum/maximum zoom scale bscrollView.minimumZoomScale= 0.5; // 0.5 means half its normal sizescrollView.maximumZoomScale= 2.0; // 2.0 means twice its normal size

    Will not work without delegate method to specify view- (UIView *)viewForZoomingInScrollView:(UIScrollView *)sender;If your scroll view only has one subview, you return it here. More than one? U

    Zooming programatically@property (nonatomic) float zoomScale;- (void)setZoomScale:(float)scale animated:(BOOL)animated;- (void)zoomToRect:(CGRect)zoomRect animated:(BOOL)animated;

  • 8/13/2019 Lecture 10 Slides

    35/44

    scrollView.zoomScale = 1.2;

  • 8/13/2019 Lecture 10 Slides

    36/44

    scrollView.zoomScale = 1.0;

  • 8/13/2019 Lecture 10 Slides

    37/44

    scrollView.zoomScale = 1.2;

  • 8/13/2019 Lecture 10 Slides

    38/44

    - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;

  • 8/13/2019 Lecture 10 Slides

    39/44

    - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;

  • 8/13/2019 Lecture 10 Slides

    40/44

    - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;

  • 8/13/2019 Lecture 10 Slides

    41/44

    - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;

    UIScrollView

  • 8/13/2019 Lecture 10 Slides

    42/44

    UIScrollViewLots and lots ofdelegate methods!

    The scroll view will keep you up to date with whats going on.

    Example: delegate method will notify you when zoomi- (void)scrollViewDidEndZooming:(UIScrollView *)sender

    withView:(UIView *)zoomView // from delegate me

    atScale:(CGFloat)scale;

    If you redraw your view at the new scale, be sure to reset the transform back

    Demo

  • 8/13/2019 Lecture 10 Slides

    43/44

    DemoImaginariumUIImageView inside a UIScrollViewMultithreaded download from a URL

    UIActivityIndicatorView to show user that a download is in progress

  • 8/13/2019 Lecture 10 Slides

    44/44