All the codes have been updated to Swift 4.1.
中文版:抓住iOS的未来 - 30天学习编写30个Swift小程序
For any problem, please send me the e-mail: egeg3000@gmail.com.
Project 30 - Google Now App
What I learned:
- How to do Transition Animation for Present/Dismiss
- How to do combine animation based on BubbleTransition
- Draw round button, the codes are:
triggerButton.layer.cornerRadius = triggerButton.frame.width / 2
triggerButton.layer.masksToBounds = true
Project 29 - Beauty Contest
What I learned
- This project is based on Yalantis的Koloda, Koloda is a very useful UIImage Selector.
- Two ways to use lazy load in Swift:
lazy var firstWay = "first"
And
lazy var secondWay: String = {return "Second"}()
Warning: For the second way, you must define the TYPE FIRST, so the complier could check in the beginning.
Project 28 - SnapChat Like App
What I learned
- Usage of UIScrollView, e.g., how to forbidden bounces property, isPagingEnabled property, and contentOffset property
- How to use addChildViewController
- How to use AVCaptureSession
Project 27: Carousel Effect (跑马灯效果)
What I learned
- Usage of UICollectionView, for example, to deal with padding within cells, you could adjust with minimumLineSpacingForSection
- To customize a layout, you need to realize such func:
prepare()
shouldInvalidateLayout(forBoundsChange newBounds: CGRect)
targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint)
layoutAttributesForElements(in rect: CGRect)
- Usage of Visual Effect View
Project 26 - Twitter-like Splash
What I learned
- Usage of CAAnimation
- reference of Keypath in CAAnimation
- add Key Animation Frame by using CAKeyFrameAnimation
Project 25 Custom Transition
What I learned
- We can customize animation of navigationcontroller, that is to say, to implement the func in
UINavigationControllerDelegate
- If we only need to focus on fromVC & toVC, we only need to implement such methods:
navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
Project 24 - Vertical Menu Transition
What I learned
- Customize naviation transition for Present/Dismiss
- Generally speaking, for target-action, we will set target to "self". But if we set target to "delegate", then the action should be set
#selector(CustomTransitionDelegate.functionName)
- In the transition Animation complete block, please insert following func:
transitionContext.completeTransition(true)
fromViewController?.endAppearanceTransition()
toViewController?.endAppearanceTransition()
Project 23 - Side Navigation App
What I learned
- Mix Swift with Objective-C
- New a head file, e.g., Bridge.h
- Click Project file, then Build Setting, then Objective-C Bridge Header, set the file path in Bridge.h
- Then import all the header files in Bridge.h
- Learned how to use SWRevealViewController
Project 22 - Basic Animations
What I learned
- For animation related to position, we can both implement by modify the origin, and by setting the transform property of UIView
- For animation related to Opacity, we can modify the alpha directly
- For animation related to Scale, set the transform property of UIView, e.g.:
heartView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
- For animation related to Color, modify the backgroundColor property
- For animation related to Rotation, set the Scale Object to transform, e.g.:
heartView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
Project 21 CoreData App
What I learned
- In order to autogenerate some codes for you, please check UseCoreData while creating new project.
- Don't have to use Editor to generate Subclass
- Reference
Project 20 - Apple Watch OS App - Guess Game
What I learned
- Need to select Watch OS while creating the project.
- Have to use Storyboard to do the layout for apple watch, and the related file is Interface.storyboard
- Use WCSession to do the interaction between Watch and Main App
- Usecase refer too:WatchKit Introduction: Building a Simple Guess Game
Project 19 - TodayWidget
What I learned
- Create Today Widget: "File > New > Target",then select the iOS section's "Application Extension's Today Extension"
- In order to share data between widget and app, please open the "App group" function. "target -> capability -> App Group -> Open it"
- Add the group and name it, then redo this steps in Extension's target, and select the group you created just now.
- Can use UserDefault as the shared storage between Main App and Widget, but we can't use StandardUserDefaults here, only to use suitName, and the name must follow the named group, e.g.:
let userDefault = UserDefaults(suiteName: "group.nimoAndHisFriend.watchDemo")
Project 18 - Spotlight Search
What I learned
- How to use Spotlight Search:
- import CoreSpotlight
- Add the object as CSSearchableItem: Create a CSSearchableItemAttributeSet, then fill it's properties, such as title, contentDescription, and thumbData
- CSSearchableItemAttribuitSet's thumbData can be catched either by UIImageJPEGRepresentation or by UIImagePNGRepresentation(Depend on the type of photo)
- Create CSSearchableItem Obj, then add to SearchIndex by indexSearchableItems:
let tmpItems = [searchItem]
CSSearchableIndex.default().indexSearchableItems(tmpItems) { (error) in
}
- If you face problems in debug, please reset the simulator, or just debug with Device.
Project 17 - 3D Touch Quick Action
- Please make sure the application can support 3D Press function:
self.window?.traitCollection.forceTouchCapability == .available
- There are two kinds of operation with 3D Press: By long press icon in Spring Board(destop in iOS), and by long press some certain element and have a Peek View in Application.
- For the first type, create the item with
UIApplicationShortcutItem
, then set the application's shortcutItems property. Please remember, you can't use custom icon. - For the second type, please add related event in the ViewController you want to:
- implement
UIViewControllerPreviewingDelegate
- register 3D Touch support :
self.registerForPreviewing(with: self, sourceView: self.view)
- implement following methods:
- implement
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController)
Project 16 - LoginAnimation
What I learned
- Bounce animation need to use ```usingSpringWithDamping````, and please pay attention to:
- usingSpringWithDamping:the less the more exaggerate
- initialSpringVelocity: the initial speed of animation
- options: you can choose the animation method, such as EaseIn, EaseOut, EaseInOut...
- ReferenceDoc1,Doc2
- This kind of animation is so amazing, I like it :P
Project 15 - Tumblr Menu
What I learned
- animation + BlurEffect
- These lines of icon's appearing has a sequence, please control it by setting different delay value
- For custom button, I refered to :Custom Button
Project 14 - Video Splash
What I learned
- Create an AVPlayerViewController, then set it as the background of View
- Then autoplay with AVPlayerViewController
- Refered to a lib called
VideoSplashViewController
Project 13: Animation In TableViewCell
What I learned
- Do the animation in method:
viewWillAppear
. Then get all the cells by visibleCells of tableView, then do enumerate it and do animation - Do animation by using
usingSpringWithDamping
, set 0.8 tousingSpringWithDamping
, 0 toinitalSpringVelocity
.
Project 12 - Emoji Slot Machine
What I learned
- Use UIPicker to do this game
- In order to avoid show the edge of the UIPickerView, the first value and the last value of UIPickerView is forbidden.
Int(arc4random())%(emojiArray.count - 2) + 1
- Add double click action to make sure it will trigger the 'bingo'
- I love this game. :)
Project 11 - Gradient in TableView
What I learned
- Use CAGradientLayer on UITableViewCell
- I suggest to use CAGradientLayer as the backgroundView of cell
- hide the useless style of UITableViewCell:
table.separatorStyle = .none
cell.selectionStyle = .none
Project 10 - Stretchy Header
What I learned
- get the offset of ScrollView by implementing
scrollViewDidScroll
of UIScrollView - then set Max and Min of the scale for picture
- set the transform of UIImageView to finish it:
bannerImgView.transform = CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)
Project 9 - Swipeable Cell
What I learned
- use
editActionsForRowAt
for UItableView, and set the functions as a array to the return value:Array<UITableViewRowAction>
- for each action please init UITableViewRowAction, and set the logic in block.
- this demo only for general usage of swipeable cell, if you want to custom the swiped button, you must customize the Cell and implement functions by UIPanGesture
Project 8 - Color Gradient
What I learned
- Use CAGradientLayer for the Gradient effect
- Color set's concept will help to make the array of color. Please pay attention hat the property is CGColor
- Changing color by scrolling is implemented by PanGestureRecognizer, and the effect and operation refered to : Solar
Project 7 - Simple Photo Browser
What I learned
- Set imageView on ScrollView to do Image Zoom
- Set maxZoomScale / minZoomScale
- implement viewForZooming method
Project 6 - Video Player
What I learned
- How to use AVPlayer, AVPlayerViewController and AVPlayerLayer
- How to run app in background by do configuration in plist
- Show info in Lock Screen(remoteControlReceived)
- Grammer: do...catch...
- Reference
Project 5 - Pull To Refresh
What I learned
- Usage of UIRefreshControll:
message:
attributedTitle
, action:UIControlEvents.valueChanged
Project 4 - Limited Input Text Field
What I learned
- limit input by capturing texting method: textViewDidChange
- By observing NSNotification.Name.UIKeyboardWillChangeFrame, we can get the pop and dismiss of system keyboard.
Project 3 - Find My Position
What I learned
-
configuration of location: in plist, please add: NSLocationAlwaysUsageDescription
-
can use CLLocationManger to do locate
-
if you face
Domain=GEOErrorDomain Code=-8 "(null)"
, please changeCLGeocoder
var to global var
Project 2: Watch Demo
What I learned
- Update cocoaPods to 1.2.0
- Learn how to use SnapKit (Quite similar with Monsary)
- Learn how to use Timer in Swift
- What I learned: guard,refer to guard
Project 1: Change Custom Font
What I learned
- How to change property of font
- we could search font name in Storyboard, or following codes:
func printAllSupportedFontNames() {
let familyNames = UIFont.familyNames
for familyName in familyNames {
print("++++++ \(familyName)")
let fontNames = UIFont.fontNames(forFamilyName: familyName)
for fontName in fontNames {
print("----- \(fontName)")
}}}