[文章]如何用swift实现运动视觉效果MotionEffect

适合iOS7及以上版本

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加最下面一层
        let lowerBackgroundImageView = UIImageView(image: UIImage(named: "bgLower.png"))
        lowerBackgroundImageView.frame = CGRectInset(view.frame, -50.0, -50.0)
        view.addSubview(lowerBackgroundImageView)
        useMotionEffect(lowerBackgroundImageView, magnitude: 50.0)

        //添加中间一层
        let midImageView = UIImageView(image: UIImage(named: "bgMid.png"))
        view.addSubview(midImageView)

        //添加最上面一层
        let foregoundImageView = UIImageView(image: UIImage(named: "bgHeader.png"))
        foregoundImageView.center = CGPointMake(view.frame.width/2, view.frame.height/2)
        view.addSubview(foregoundImageView)
        useMotionEffect(foregoundImageView, magnitude: -20.0)
    }

    func useMotionEffect(view:UIView,magnitude:CGFloat) {
        let xMotion = UIInterpolatingMotionEffect(keyPath: "center.x", type: UIInterpolatingMotionEffectType.TiltAlongHorizontalAxis)
        xMotion.minimumRelativeValue = -magnitude
        xMotion.maximumRelativeValue = magnitude

        let yMotion = UIInterpolatingMotionEffect(keyPath: "center.y", type: UIInterpolatingMotionEffectType.TiltAlongVerticalAxis)
        yMotion.minimumRelativeValue = -magnitude
        yMotion.maximumRelativeValue = magnitude

        let group = UIMotionEffectGroup()
        group.motionEffects = [xMotion,yMotion]
        view.addMotionEffect(group)
    }

[文章]如何用Swift写UIDynamic

记录下

import UIKit

class FirstViewController: UIViewController,UICollisionBehaviorDelegate {
    var animator = UIDynamicAnimator()
    var gravity = UIGravityBehavior()
    var collision = UICollisionBehavior()
    var firstContact = false

    override func viewDidLoad() {
        super.viewDidLoad()

        let square = UIView(frame: CGRectMake(100, 100, 100, 100))
        square.backgroundColor = UIColor.grayColor()
        view.addSubview(square)

        animator = UIDynamicAnimator(referenceView: view)
        gravity = UIGravityBehavior(items: [square])
        animator.addBehavior(gravity)

        let barrier = UIView(frame: CGRectMake(0, 300, 130, 20))
        barrier.backgroundColor = UIColor.redColor()
        view.addSubview(barrier)

        collision = UICollisionBehavior(items: [square])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)

        //添加一个碰撞体
        let rightEdge = CGPointMake(barrier.frame.origin.x + barrier.frame.size.width, barrier.frame.origin.y)
        collision.addBoundaryWithIdentifier("barrier", fromPoint: barrier.frame.origin, toPoint: rightEdge)

        collision.action = {
            println("(NSStringFromCGAffineTransform(square.transform)),(NSStringFromCGPoint(square.center))") //只要动就会触发,可以获得动的物体的位置
        }
        collision.collisionDelegate = self

        let itemBehaviour = UIDynamicItemBehavior(items: [square])
        itemBehaviour.elasticity = 0.6 //更改弹性
        animator.addBehavior(itemBehaviour)
    }

    func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying, atPoint p: CGPoint) {
        println("began contact") //碰撞时触发
        //每次碰撞都会变黄一下,然后又转成灰色
        let view = item as UIView
        view.backgroundColor = UIColor.yellowColor()
        UIView.animateWithDuration(0.3, animations: {
            view.backgroundColor = UIColor.grayColor()
        })

        //检测第一次碰撞,动态添加behaviors
        if !firstContact {
            firstContact = true
            //处理第一次碰撞的地方
        }

    }

[文章]UIFont使用最佳实践

UIFont使用最佳实践是使用perferred font
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
还有一些其它的style UIFontTextStyleHeadLine,UIFontTextStyleCaption1,UIFontTextStyleFootnote等

[绘画]乔弗里·拜拉席恩,布兰·史塔克,珊莎·史塔克

[绘画]微博娃娃新icon和一些其它的练习