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:
////YYVIEWCONTROLLER.M//13-Snapping behavior////Created by Apple on 14-8-8.//Copyright (c) 2014 Yangyong. All rights reserved.//#import"YYViewController.h"@interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet UIView*Blueview, @property (nonatomic,strong) Uidynamicanimator*animator, @end @implementation Yyviewcontroller-(Uidynamicanimator *) animator{if(_animator==Nil) { //Create a physical emulator, set the simulation scope, Referenceview as the reference view_animator=[[Uidynamicanimator Alloc]initwithreferenceview:self.view]; } return_animator;}- (void) viewdidload{[Super Viewdidload];}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //get a touch pointUitouch *touch=[touches anyobject]; Cgpoint Point=[Touch LocationInView:touch.view]; //1. Create snapping behavior//you need to pass in two parameters: a physical simulation element, a snap pointUisnapbehavior *snap=[[Uisnapbehavior Alloc]initwithitem:self.blueview Snaptopoint:point]; //Set the vibration factor (0~1, the larger the value, the smaller the amplitude of the vibration)Snap.damping=arc4random_uniform (Ten)/10.0; //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];} @end
iOS Development Extension Chapter-uidynamic (snapping behavior)