iOS Development Extension Chapter-uidynamic (snapping behavior)

Source: Internet
Author: User

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

iOS Development Extension Chapter-uidynamic (snapping behavior)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.