iOS Development Extension Chapter-uidynamic (snapping behavior)
First, Introduction
The object can be quickly rushed to a position (snap position), the position will be captured with a certain vibration
Initialization of the Uisnapbehavior
-(Instancetype) Initwithitem: (ID <UIDynamicItem>) Item snaptopoint: (Cgpoint) point;
Uisnapbehavior Common Properties
@property (nonatomic, assign) CGFloat damping;
For reduction, damping (value range is 0.0 ~ 1.0, the greater the value, the smaller the amplitude of vibration)
Uisnapbehavior Use note
If you want to perform a continuous snapping behavior, you need to remove the preceding snapping behavior from the physical emulator first
Second, the code description
Put a view control in storyboard as the simulation element for the demo.
The code is as follows:
1 //2 //YYVIEWCONTROLLER.M3 //13-Snapping behavior4 //5 //Created by Apple on 14-8-8.6 //Copyright (c) 2014 Yangyong. All rights reserved.7 //8 9 #import "YYViewController.h"Ten One @interfaceYyviewcontroller () A@property (Weak, nonatomic) Iboutlet UIView *Blueview; -@property (nonatomic,strong) Uidynamicanimator *animator; - @end the - @implementationYyviewcontroller - --(Uidynamicanimator *) Animator + { - if(_animator==Nil) { + //Create a physical emulator, set the simulation scope, Referenceview as the reference view A_animator=[[Uidynamicanimator Alloc]initwithreferenceview:self.view]; at } - return_animator; - } -- (void) Viewdidload - { - [Super Viewdidload]; in } --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event to { + //get a touch point -Uitouch *touch=[touches anyobject]; theCgpoint point=[Touch LocationInView:touch.view]; * $ //1. Create snapping behaviorPanax Notoginseng //you need to pass in two parameters: a physical simulation element, a snap point -Uisnapbehavior *snap=[[Uisnapbehavior Alloc]initwithitem:self.blueview snaptopoint:point]; the //Set the vibration factor (0~1, the larger the value, the smaller the amplitude of the vibration) +Snap.damping=arc4random_uniform (Ten)/10.0; A the //2. Perform snapping behavior + //Note: This control can only be used in a simulation behavior, if you want to have a continuous simulation behavior, then you need to remove all the previous simulation behavior - //Remove all previous emulation behavior $ [Self.animator removeallbehaviors]; $ [Self.animator Addbehavior:snap]; - } - the @end