我的IOS成长之路——by Swift

不定期更新-----

希望学IOS的朋友能跟我多多交流,分享经验

IOS UI控件

1.UIImagePickerController

官方解释

The UIImagePickerController class manages customizable, system-supplied user interfaces for taking pictures and movies on supported devices, and for choosing saved images and movies for use in your app. An image picker controller manages user interactions and delivers the results of those interactions to a delegate object.

简单一点就是可以用来访问设备上的图片和视频,调用也很简单

let imagePicker = UIImagePickerController()
imagePicker.delegate = self
self.presentViewController(imagePicker, animated: true, completion: nil)



要使用delegate就需要实现 UIImagePickerControllerDelegate , UINavigationControllerDelegate这两个协议,主要是重写第一个协议中的两个各方法(第二个协议我也不知道来干什么,麻烦知道的朋友指点一下)

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        //user cancel operate
        print("choose image cancel")
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let picker = info[UIImagePickerControllerOriginalImage] as! UIImage
        image.image = picker
        self.dismissViewControllerAnimated(true, completion: nil)
    }

2.UIDynamicAnimator

A dynamic animator provides physics-related capabilities and animations for its dynamic items, and provides the context for those animations. It does this by intermediating between the underlying iOS physics engine and dynamic items, via behavior objects you add to the animator.

这是一个提供遵循物理定律的动画,有点类似游戏中的物理引擎,具体使用

class AnimationViewController: UIViewController, UICollisionBehaviorDelegate {

    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!
    @IBOutlet weak var btn3: UIButton!
    
    
    var collision: UICollisionBehavior!
    var animator: UIDynamicAnimator!
    var attachmentBehavior: UIAttachmentBehavior!
    
    @IBAction func btn1Action(sender: AnyObject) {
        animator.removeAllBehaviors()
        let gravity = UIGravityBehavior(items: [self.btn1,self.btn2,self.btn3])
        animator.addBehavior(gravity)
        collision = UICollisionBehavior(items: [self.btn1,self.btn2,self.btn3])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
    }
    
    
    @IBAction func btn2Action(sender: AnyObject) {
        animator.removeAllBehaviors()
        let push = UIPushBehavior(items: [self.btn1,self.btn2,self.btn3], mode:UIPushBehaviorMode.Instantaneous)
        push.magnitude = 2
        animator.addBehavior(push)
        collision = UICollisionBehavior(items: [self.btn1,self.btn2,self.btn3])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
    }
    //停止所有动作
    @IBAction func btn3Action(sender: AnyObject) {
        animator.removeAllBehaviors()
        
        let anchorPoint = CGPointMake(self.btn3.center.x, self.btn3.center.y)
        attachmentBehavior = UIAttachmentBehavior(item: self.btn3, attachedToAnchor: anchorPoint)
        attachmentBehavior!.frequency = 0.5
        attachmentBehavior!.damping = 2
        attachmentBehavior!.length = 20
        animator.addBehavior(attachmentBehavior!)
        collision = UICollisionBehavior(items: [self.btn1, self.btn2, self.btn3])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        animator = UIDynamicAnimator(referenceView: self.view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
    override func viewDidAppear(animated: Bool) {
        let max: CGRect = UIScreen.mainScreen().bounds
        let snap1 = UISnapBehavior(item: self.btn1, snapToPoint: CGPointMake(max.size.width/2, max.size.height/2 - 50))
        let snap2 = UISnapBehavior(item: self.btn2, snapToPoint: CGPointMake(max.size.width/2, max.size.height/2 ))
        let snap3 = UISnapBehavior(item: self.btn3, snapToPoint: CGPointMake(max.size.width/2, max.size.height/2 + 50))
        snap1.damping = 1
        snap2.damping = 2
        snap3.damping = 4
        animator.addBehavior(snap1)
        animator.addBehavior(snap2)
        animator.addBehavior(snap3)
    }
    */
}



你可能感兴趣的:(ios,UI,swift)