iOS 7 SDK特訓班
Upcoming SlideShare
Loading in...5
×
 

iOS 7 SDK特訓班

on

  • 2,726 views

agenda: ...

agenda:

iOS 7 SDK簡介
Xcode 5新功能簡介
更進步的Objective-C和Foundation framework
iOS 6 App改版iOS 7 (UI調整)
動態文字大小
酷炫動畫1: Motion Effects (3D)
酷炫動畫2: UIKit Dynamics (2D)

Statistics

Views

Total Views
2,726
Views on SlideShare
2,726
Embed Views
0

Actions

Likes
15
Downloads
63
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

iOS 7 SDK特訓班 iOS 7 SDK特訓班 Document Transcript

  • iOS 7 SDK 彼得潘 • • • :App - iPhone . iPad • 2012 Top 6 App
  • APP Mac http://www.facebook.com/iphone.peterpan http://deeploveapple.blogspot.tw apppeterpan@gmail.com http://apppeterpan.blogspot.tw FB blog wiki http://peterpan.uservoice.com http://deeploveiossdk.tumblr.com wiki email
  • agenda • iOS 7 SDK • Xcode 5 • Objective-C Foundation framework • iOS 6 App iOS 7 (UI ) • • 1: Motion Effects (3D) • 2: UIKit Dynamics (2D) iOS 7 SDK
  • • UI • • • • device • • • • • device • App • JavaScript • iBeacon • Machine Readable Code Detection • Apple Map • Safari Reading list • UI • UI • Button • nav bar search bar • blurred background ( ) • image rendering mode (App iOS 7 ) • tint color (App iOS 7 ) • full screen status bar & navigation bar (App iOS 7 )
  • UI button nav bar search bar UISearchBar *searchBar = [[UISearchBar alloc] init]; searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; searchController.searchResultsDataSource = self; searchController.searchResultsDelegate = self; searchController.delegate = self; searchController.displaysSearchBarInNavigationBar = YES; searchController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(close:)]; UISearchDisplayController.h @property (nonatomic, assign) BOOL displaysSearchBarInNavigationBar; UINavigationController UINavigationBar
  • blurred background ( ) https://developer.apple.com/downloads/index.action UIImage+ImageEffects - (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage; UIToolbar - (void)viewDidLoad { [super viewDidLoad]; ! UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)]; [self.view addSubview:scrollView]; scrollView.contentSize = CGSizeMake(320, 1000); UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 1000)]; imageView.image = [UIImage imageNamed:@"cute.jpg"]; [scrollView addSubview:imageView]; UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)]; [self.view addSubview:toolBar]; }
  • UIToolbar UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)]; [self.view addSubview:toolBar]; toolBar.barTintColor = [UIColor blueColor]; • view controller • CollectionView • interactive transition • Motion Effects ( ) • UIKit Dynamics ( )
  • view controller http://www.doubleencore.com/2013/09/ios-7-custom-transitions http://www.appdesignvault.com/custom-transition-ios-7 http://blog.bignerdranch.com/3871-golden-opportunity-custom- transitions/ UIViewControllerAnimatedTransitioning protocol CollectionView WWDC 2013 Sample: iOS_CollectionViewTransition ex: App YearsView Collections zoom in https://developer.apple.com/downloads/index.action?name=WWDC%202013
  • interactive transition • controller • ex: scroll UIViewControllerInteractiveTransitioning Multitasking Enhancements • (Background Fetch) • ( Remote Notification ) silent push ( )
  • App Remote Notification
  • Text Kit Letterpress ( ) Letterpress Letterpress
  • letterpress UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; label.font = [UIFont systemFontOfSize:150]; label.textAlignment = NSTextAlignmentCenter; NSDictionary *attrDic = @{NSForegroundColorAttributeName : [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5], NSTextEffectAttributeName : NSTextEffectLetterpressStyle}; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@" " attributes:attrDic]; label.attributedText = attrString; [self.view addSubview:label]; NSAttributedString.h UIKIT_EXTERN NSString *const NSTextEffectLetterpressStyle NS_AVAILABLE_IOS(7_0); Exclusion paths
  • Exclusion paths UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, 320, 400)]; textView.backgroundColor = [UIColor clearColor]; textView.textColor = [UIColor whiteColor]; textView.text = @" "; textView.font = [UIFont systemFontOfSize:28]; UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(10.0, 40.0)]; [path addLineToPoint:CGPointMake(110.0, 140.0)]; [path addLineToPoint:CGPointMake(210, 40)]; [path closePath]; textView.textContainer.exclusionPaths = @[path]; [self.view addSubview:textView]; NSTextContainer.h @property(copy, NS_NONATOMIC_IOSONLY) NSArray *exclusionPaths; Text attachments
  • Text attachments UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, 320, 400)]; textView.backgroundColor = [UIColor clearColor]; NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@" "]; NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"shoppingIcon.png"]; [str appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; [str appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@" "]]; textView.attributedText = str; [self.view addSubview:textView]; NSTextAttachment.h @interface NSAttributedString (NSAttributedStringAttachmentConveniences) // A convenience method for creating an attributed string containing attachment using NSAttachmentCharacter as the base character. + (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment NS_AVAILABLE_IOS(7_0); @end dynamic text size
  • AirDrop available on iPhone 5, iPad (4th generation), iPad mini, and iPod touch (5th generation) requires an iCloud account :Wifi Bluetooth AirDrop • UIActivityViewController • Share • NSString & NSAttributedString ( Notes.app ) • UIImage & AVAsset (Photos.app ) • NSURL • Safari.app • App
  • Machine Readable Code Detection support barcode • QR code • Aztec • EAN13 • EAN8 • UPC-E • PDF417 • Code 93 • Code 39 • Code 39 mod 43 AVCaptureMetadataOutput
  • SSReadingList Safari Reading list NSURL *url = [NSURL URLWithString:@"https:/ /www.facebook.com/iphone.peterpan"]; [[SSReadingList defaultReadingList] addReadingListItemWithURL:url title:@" " previewText:@" " error:nil]; • CIDetectorSmile • CIDetectorEyeBlink
  • Multipeer Connectivity • device • : infrastructure Wi-Fi networks peer-to-peer Wi-Fi Bluetooth Multipeer Connectivity
  • Inter-App Audio • App MIDI • App iBeacon bluetooth beacon : iPhone CLBeaconRegion
  • Map Kit MKOverlayRenderer overlay
  • MKOverlayRenderer Direction
  • AVSpeechSynthesis AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@" "]; [synthesizer speakUtterance:utterance]; AVSpeechSynthesizer AVSpeechSynthesizer: • M7 • 5s support • CMStepCounter • typedef void (^CMStepUpdateHandler)(NSInteger numberOfSteps, NSDate *timestamp, NSError *error);
  • Sprite Kit • OpenGL ES • library Cocos 2D • 2D Game Controller : Bluetooth
  • Game Controller Game Controller
  • JavaScriptCore • objective-c javascript • javascript objective-c Xcode 5
  • Xcode 5 • UI • • Asset Catalogs • Image Slicing • • App • Interface Builder Preview • support 64 bit iOS (iPhone 5s) • • App Capability • Debug Gauges • XCTest • Continuous Integration • Autolayout • Quick Look • Xcode • OS X Mavericks
  • template Asset Catalogs & Image Slicing • • ex: @2x, ~ipad • • App Icon
  • branch branch Debug Gauges
  • App App Capability Management
  • Quick Look debug Preview
  • Preview /** sing a song @code ViewController controller = [ViewController alloc] init]; [controller singSong:@" 89 "]; @endcode @see http://www.apple.com for more info @param song the song to sing @return YES if song name lenght > 3 */ -(BOOL)singSong:(NSString*)song { if (song.length > 3) { return YES; } else { return NO; } }
  • IB auto layout • IB constraint • constraint XCTest
  • Continuous Integration • build App • simulator • OS X Mavericks Server • • • on commit • periodically Continuous Integration
  • Objective-C Foundation framework Objective-C Foundation framework • module • instancetype • NSArray
  • module • SDK framework • • import framework
  • module : MapKit.framework module !
  • @import • @import QuartzCore; • @import QuartzCore.CoreAnimation; • #import @import ex: (1) #import <UIKit/UIKit.h> @import UIKit; (2) #import <iAd/ADBannerView.h> @import iAd.ADBannerView; • module #import enable modules
  • instancetype id NSArray.h + (id)array; NSDictionary *dic = [NSArray array];
  • instancetype NSArray.h + (instancetype)array; return type array NSArray.h - (id)firstObject NS_AVAILABLE(10_6, 4_0); - (id)lastObject; firstObject iOS 7 NSArray *array = [[NSArray alloc] init]; NSLog(@"array %@", array[0]); NSArray *array = [[NSArray alloc] init]; NSLog(@"array %@", [array firstObject]); nil
  • iOS 6 App iOS 7 iOS 7 iOS 6
  • iOS 6 App iOS 7 • App Icon • Asset Catalogs Image Slicing • tint color • iOS • full screen status bar & navigation bar • image render mode • interactivePopGestureRecognizer • keyboard , picker & alert view App Icon size App Icon (pixel) iPhone 57 * 57 iPhone Retina 114 * 114 iPad (iPad mini) 72 * 72 iPad Retina 144 * 144 iPhone Retina iOS 7 120 * 120 iPad (iPad mini) iOS 7 76 * 76 iPad Retina iOS 7 152 * 152
  • iOS 7 Retina App iPhone App iOS 7 -> retina size Launch Image (pixel) iPhone 3.5 320 * 480 iPhone Retina 3.5 640 * 960 iPhone Retina 4 640 * 1136 iPad (iPad mini) 768 * 1024 1024 * 768 iPad Retina 1536 * 2048 2048 * 1536 full screen status bar
  • App Icon asset catalogs Use Asset Catalog asset catalogs
  • asset catalogs image.xcassets AppIcon
  • iOS AppIcon attributes inspectorps: Development Target • support png • • Asset Catalogs andy.png tony.png self.imageView.image = [UIImage imageNamed:@"handsome"];
  • Asset Catalogs andy.png tony.png chilam.png tai.pnglin.png Image Slicing
  • tint color tint color • UI • active UI tab • UI (ex: UIButton ) • button
  • UIView tintColor property • default: blue • tintColor : 1. 2. superview tintColor superview 3. superview tintColor • App 1. UI tint color 2. App tint color UIView.h @property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(7_0); view superview tintColor App tint color • window tintColor • storyboard global tint
  • tint color self.view.tintColor = [UIColor orangeColor]; iOS 6 Crash tintColor property iOS 7 iOS if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { } else { } NSObjCRuntime.h #define NSFoundationVersionNumber_iPhoneOS_2_0!678.24 #define NSFoundationVersionNumber_iPhoneOS_2_1 678.26 #define NSFoundationVersionNumber_iPhoneOS_2_2 678.29 #define NSFoundationVersionNumber_iPhoneOS_3_0 678.47 #define NSFoundationVersionNumber_iPhoneOS_3_1 678.51 #define NSFoundationVersionNumber_iPhoneOS_3_2 678.60 #define NSFoundationVersionNumber_iOS_4_0 751.32 #define NSFoundationVersionNumber_iOS_4_1 751.37 #define NSFoundationVersionNumber_iOS_4_2 751.49 #define NSFoundationVersionNumber_iOS_4_3 751.49 #define NSFoundationVersionNumber_iOS_5_0 881.00 #define NSFoundationVersionNumber_iOS_5_1 890.10 #define NSFoundationVersionNumber_iOS_6_0 993.00 #define NSFoundationVersionNumber_iOS_6_1 993.00 iOS SDK: crash iOS deprecated SDK: do nothing
  • iOS 6 tintColor UINavigationBar.h @property(nonatomic,retain) UIColor *tintColor; @property(nonatomic,retain) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; iOS 7 barTintColor full screen status bar navigation bar cell: UITableViewStyleGrouped
  • view controller’s view size • iOS 7 view size full screen • iOS 6 view size full screen • status bar black translucent • controller wantsFullScreenLayout YES • navigation bar translucent YES controller 320 * 568
  • nav bar & tab bar nav bar controller controller tab bar ( 519 = 568 - 49) Extend Edges
  • bar translucent nva bar translucent Under Top bars tab bar translucent Under Bottom bars Under Opaque Bars
  • bar 455 = 568 - 20 - 44 -49 UIViewController.h @property(nonatomic,assign) UIRectEdge edgesForExtendedLayout; @property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars; UIGeometry.h typedef NS_OPTIONS(NSUInteger, UIRectEdge) { UIRectEdgeNone = 0, UIRectEdgeTop = 1 << 0, UIRectEdgeLeft = 1 << 1, UIRectEdgeBottom = 1 << 2, UIRectEdgeRight = 1 << 3, UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight };
  • - (void)viewDidLoad { [super viewDidLoad]; ! // Do any additional setup after loading the view, typically from a nib. self.extendedLayoutIncludesOpaqueBars =YES; self.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeBottom; } scroll view inset scroll view, table view, web view
  • automaticallyAdjustsScrollViewInsets UIViewController.h @property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets; UIScrollView.h @property(nonatomic) UIEdgeInsets contentInset; UIGeometry.h typedef struct UIEdgeInsets { CGFloat top, left, bottom, right; } UIEdgeInsets; inset {64, 0, 0, 0} content start from 64 automaticallyAdjustsScrollViewInsets NO
  • full screen • iOS 6 : (0, 0) • iOS 7: • only status bar : (0, 20) • status bar + nav bar: (0, 64) topLayoutGuide & bottomLayoutGuide topLayoutGuide nav bar
  • topLayoutGuide & bottomLayoutGuide topLayoutGuide status bar topLayoutGuide & bottomLayoutGuide - (void)viewDidLoad { [super viewDidLoad]; UIView *blueView = [[UIView alloc] initWithFrame:CGRectZero]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; blueView.translatesAutoresizingMaskIntoConstraints = NO; id topLayoutGuide = self.topLayoutGuide; NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(blueView, topLayoutGuide); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V: [topLayoutGuide]-10-[blueView(100)]" options:0 metrics:nil views:viewsDictionary]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[blueView(100)]" options:0 metrics:nil views:viewsDictionary]]; }
  • bar UIBarPositionAny UIBarPositionBottom UIBarPositionTop UIBarPositionTopAttached - (void)viewDidLoad { [super viewDidLoad]; ! UIImage *image = [UIImage imageNamed:@"bar.png"]; UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)]; [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; [self.view addSubview:navBar]; navBar.delegate = self; } - (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar { return UIBarPositionTopAttached; } status bar : status bar
  • : - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleDefault; } ( ) method controller status bar controller default status bar - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; return YES; } status bar controller View controller-based status bar appearance (UIViewControllerBasedStatusBarAppearance) NO setStatusBarStyle:
  • hide status bar view controller [[UIApplication sharedApplication] setStatusBarHidden:isHide withAnimation:UIStatusBarAnimationFade]; ! hide status bar - (BOOL)prefersStatusBarHidden { return YES; } controller status bar
  • controller [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; work ! status bar nav bar • scroll • 44 64 • nav bar 64 point
  • bar button item UIImage *image = [UIImage imageNamed:@"foodIcon.png"]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(showSetting:)]; self.navigationItem.rightBarButtonItem = barButtonItem; iOS 7 iOS 6 initWithCustomView image render mode UIImage.h typedef NS_ENUM(NSInteger, UIImageRenderingMode) { UIImageRenderingModeAutomatic, UIImageRenderingModeAlwaysOriginal, UIImageRenderingModeAlwaysTemplate, } UIImageRenderingModeAlwaysTemplate: tint color
  • UINavigationController scroll • scroll • • ex: • : UINavigationController.h @property(nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer; self.navigationController.interactivePopGestureRecognizer.enabled = NO; delete
  • tab bar image UITabBarItem.h @property(nonatomic,retain) UIImage *selectedImage; tint color tab keyboard default UITextInputTraits.h UIKeyboardAppearanceDefault UIKeyboardAppearanceDark UIKeyboardAppearanceLight textField.keyboardAppearance = UIKeyboardAppearanceDark;
  • keyboard UIScrollView.h typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) { UIScrollViewKeyboardDismissModeNone, UIScrollViewKeyboardDismissModeOnDrag, UIScrollViewKeyboardDismissModeInteractive, } scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; UIScrollViewKeyboardDismissModeOnDrag: scroll (ex: ) UIScrollViewKeyboardDismissModeInteractive: scroll (ex: ) Picker self.picker.backgroundColor = [UIColor whiteColor]; :
  • picker UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 100, 30)]; textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:textField]; UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame: CGRectMake(0, 0, 320, 200)]; textField.inputView = picker; alert view UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alertView show]; UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; blueView.backgroundColor = [UIColor blueColor]; [alertView addSubview:blueView]; addSubview ! UIAlertView.h typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0, UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, UIAlertViewStyleLoginAndPasswordInput };
  • Text Size
  • Text Styles 1 2 3
  • UIFontDescriptor.h UIFontTextStyleHeadline UIFontTextStyleSubheadline UIFontTextStyleBody UIFontTextStyleFootnote UIFontTextStyleCaption1 UIFontTextStyleCaption2 self.nameLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; no change ?
  • App -(void)updateTextSize { self.nameLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; } - (void)viewDidLoad { [super viewDidLoad]; ! // Do any additional setup after loading the view, typically from a nib. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTextSize) name:UIContentSizeCategoryDidChangeNotification object:nil]; } :
  • -(void)updateTextSize { UIFontDescriptor *userFont = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline]; float userFontSize = [userFont pointSize]; self.nameLabel.font = [UIFont fontWithName:@"Chalkduster" size:userFontSize]; } - (void)viewDidLoad { [super viewDidLoad]; UIFontDescriptor *userFont = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline]; float userFontSize = [userFont pointSize]; self.nameLabel.font = [UIFont fontWithName:@"Chalkduster" size:userFontSize]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTextSize) name:UIContentSizeCategoryDidChangeNotification object:nil]; } preferredFontDescriptorWithTextStyle: layout • : auto layout ex: label size size • : size ex: UIFont *nameLabelFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; CGSize nameLabelFontSize = [@"Peter Pan" sizeWithAttributes: @{NSFontAttributeName: nameLabelFont}];
  • Motion Effects Parallax effect 3D
  • UIInterpolatingMotionEffect - (void)viewDidLoad { [super viewDidLoad]; ! UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; horizontalMotionEffect.minimumRelativeValue = @-50; horizontalMotionEffect.maximumRelativeValue = @50; [self.imageView addMotionEffect:horizontalMotionEffect]; } UIMotionEffect demo
  • horizontalMotionEffect.minimumRelativeValue = @-500; horizontalMotionEffect.maximumRelativeValue = @500; - (void)viewDidLoad { [super viewDidLoad]; UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; horizontalMotionEffect.minimumRelativeValue = @-50; horizontalMotionEffect.maximumRelativeValue = @50; [self.imageView addMotionEffect:horizontalMotionEffect]; UIInterpolatingMotionEffect *verticalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis]; verticalMotionEffect.minimumRelativeValue = @-50; verticalMotionEffect.maximumRelativeValue = @50; [self.imageView addMotionEffect:verticalMotionEffect]; }
  • UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init]; group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect]; [self.imageView addMotionEffect:group]; modify CALayer - (void)viewDidLoad { [super viewDidLoad]; self.imageView.layer.borderColor = [UIColor yellowColor].CGColor; self.imageView.layer.borderWidth = 20; UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"layer.borderWidth" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; horizontalMotionEffect.minimumRelativeValue = @-20; horizontalMotionEffect.maximumRelativeValue = @20; [self.imageView addMotionEffect:horizontalMotionEffect]; }
  • motion effect • UIMotionEffect • keyPathsAndRelativeValuesForViewerOffset: http://www.teehanlax.com/blog/introduction-to-uimotioneffect/
  • 3D UIKit Dynamics
  • iOS 7 • Core Animation: • but ... • • UIAnimator: physical engine • UIDynamicBehavior: UIAnimator • UIDynamicItem protocol: UIDynamicItem protocol UIDynamicBehavior • ReferenceView: UIAnimator 2D
  • • Gravity • Collision • Attachments • Snap • Forces • Item properties UIDynamicItem @interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem> {
  • Architecture Apple Sample Code UIKit Dynamics Catalog https://developer.apple.com/library/ios/samplecode/DynamicsCatalog
  • APLGravityViewController - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[self.square1]]; [animator addBehavior:gravityBeahvior]; self.animator = animator; } @interface APLGravityViewController () @property (nonatomic, weak) IBOutlet UIImageView *square1; @property (nonatomic, strong) UIDynamicAnimator *animator; @end
  • • UIDynamicAnimator • UIGravityBehavior UIGravityBehavior • UIGravityBehavior UIDynamicAnimator ! UIGravityBehavior
  • gravityBeahvior.gravityDirection = CGVectorMake(1, -1); - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[self.square1]]; [animator addBehavior:gravityBeahvior]; UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.square1]]; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; collisionBehavior.collisionDelegate = self; [animator addBehavior:collisionBehavior]; self.animator = animator; } APLCollisionGravityViewController
  • - (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint: (CGPoint)p { [(UIView*)item setTintColor:[UIColor lightGrayColor]]; } - (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier { [(UIView*)item setTintColor:[UIColor darkGrayColor]]; }
  • UIView *square2 = [[UIView alloc] initWithFrame: CGRectMake(160, 300, 100, 100)]; square2.backgroundColor = [UIColor blueColor]; [self.view addSubview:square2]; UIKit Dynamics UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.square1, square2]];
  • Item Property - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[self.square1, self.square2]]; UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.square1, self.square2]]; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; self.square2PropertiesBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.square2]]; self.square2PropertiesBehavior.elasticity = 0.5; self.square1PropertiesBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.square1]]; [animator addBehavior:self.square1PropertiesBehavior]; [animator addBehavior:self.square2PropertiesBehavior]; [animator addBehavior:gravityBeahvior]; [animator addBehavior:collisionBehavior]; self.animator = animator; } APLItemPropertiesViewController item property • elasticity: 0 ~ 1 • friction: 0 ~ 1 • density: 1 • resistance: 0 • angularResistance: • allowsRotation: rotate
  • Replay - (IBAction)replayAction:(id)sender { // Moving an item does not reset its velocity. Here we do that manually // using the dynamic item behaviors, adding the inverse velocity for each // square. [self.square1PropertiesBehavior addLinearVelocity:CGPointMake(0, -1 * [self.square1PropertiesBehavior linearVelocityForItem:self.square1].y) forItem:self.square1]; self.square1.center = CGPointMake(90, 171); [self.animator updateItemUsingCurrentState:self.square1]; [self.square2PropertiesBehavior addLinearVelocity:CGPointMake(0, -1 * [self.square2PropertiesBehavior linearVelocityForItem:self.square2].y) forItem:self.square2]; self.square2.center = CGPointMake(230, 171); [self.animator updateItemUsingCurrentState:self.square2]; }
  • Combining behaviors addChildBehavior - (instancetype)initWithWeight:(id<UIDynamicItem>)item suspendedFromPoint:(CGPoint)p { self = [super init]; if (self) { UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[item]]; UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:p]; UIAttachmentBehavior *draggingBehavior = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:CGPointZero]; UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[item] mode:UIPushBehaviorModeInstantaneous]; pushBehavior.active = NO; [self addChildBehavior:gravityBehavior]; [self addChildBehavior:attachmentBehavior]; [self addChildBehavior:pushBehavior]; self.draggingBehavior = draggingBehavior; self.pushBehavior = pushBehavior; } return self; } APLPendulumBehavior https://github.com/shu223/iOS7-Sampler