OC Development _storyboard--block and animations

Source: Internet
Author: User

First, the agreement

@optional: Optional

@requied: Must be implemented

Second, block code blocks

1. Start with a ^, then a parameter, then a curly brace that contains our code block

1 [adictionary enumeratekeysandobjectsusingblock:^ (ID key, id value, BOOL *stop) {2      for  is%@ ", key, value); 3     if ([@ "Enough" Isequaltostring:key]) {4         *stop = YES; 5     }6}];

2, if the variable is preceded by a __block, then this variable will be moved from the stack to the heap

1 __blockBOOL stoppedearly =NO;2 DoubleStopvalue =53.5;3[Adictionary enumeratekeysandobjectsusingblock:^ (IDKeyIDValue, BOOL *stop) {4NSLog (@ "value forKey%@ is%@ ", key, value);5     if([@ "Enough" Isequaltostring:key] | | ([value doublevalue] = =stopvalue)) {6*stop =YES;7         stoppedearly = YES; // This was legal  now8     }9 }];Ten  if (stoppedearly) NSLog (@ "I stopped logging dictionary values early!");

3. Storage loop: As long as the block has all the objects in the block, there is a strong pointer pointing to them

As follows: Block has a strong pointer to self, and self also has a strong pointer to block (Self.myblock)

1 [self.myblocks addobject:^ {2    [ self dosomething]; 3 }];

The workaround is to take a __weak type, which is a weak type, which is not stored in the heap, and no pointer is pointing to nil

1 __weak // Self   was strong,  Weakself is  weak2 [self.myblocks addobject:^ {3     [weakself dosomething]; 4 }];

4. Application: Enumeration, view animation, sorting, notification, error handling, background threading (multithreading)

Third, animation

1, three ways: Fame\transform\alpha

1 + (void)animatewithduration:(nstimeinterval) duration2                       Delay: (nstimeinterval) delay3                    options: (uiviewanimationoptions) options4                  animations: (void (^) (void)) animations 5                  Completion: (void (^) (BOOL finished)) completion;

Example:

1 animatewithduration:3.0//REP duration 2                       delay:3                    // Intercept: The option here represents the 4 0.0 from the current state, rather than starting from scratch                    ;}  //implementation of modifications, etc.  5                  if //bool to determine if an animation is complete

2, IOS7 New

(1 Create a new uidunamicanimator

(2 adding dynamic behavior: e.g. gravity, collisions, propulsion, etc.)

(3 Add Content uidynamicitems (these items must implement the Protocol)

1Uidynamicanimator *animator =[[Uidynamicanimator alloc] initwithreferenceview:aview]; //Create a new Uidunamicanimator 2 3Uigravitybehavior *gravity =[[Uigravitybehavior alloc] init]; //Add behavior 4 [animator Addbehavior:gravity];5 6 ID<UIDynamicItem> item1 =  ... ; //Add content 7 ID<UIDynamicItem> item2 =  ... ;8[Gravity additem:item1];
[Gravity ADDITEM:ITEM2];

If animator is moving but you want it to rotate at the same time, you must call the updateitmeusingcurrentstate method, otherwise it will assume that the current state is known because of the performance relationship

3. Specific behavior

(1 Uigravitybehavior//Gravity behavior: The default direction is down

1 @property cgfloat angle       // angle 2 @property cgfloat magnitude   // Gravity size (1 is the gravitational acceleration)

(2 Uicollisionbehavior//Collision behavior

1 @property uicollisionbehaviormode collisionmode  // Power items: For example, colliding with each other or just bouncing off the border 2 // Whether it is an elastic boundary

(3 Uiattachmentbehavior//adsorption behavior: it can be used to adsorb a power item to a fixed stroke or another power item.

1 // initialization does not use allocation or INIT, but instead specifies the adsorption object at initialization, such as with another power item, a stroke, or an oblique way. 2 // the distance between two adsorption items 3 @property cgpoint anchorpoint

(4 Uisnapbehavior//Speed swing: To make a power item fly past a position, may not stop immediately after arrival, may be like a spring shaking

1  // initialization is also done by specifying the point to be flown 2  -(Instancetype) Initwithitme: (ID <UIDynamicItme> Item) Snaptopoint: (cgpoint) Point 3  

(5 uipushbehavior//Push behavior

1   // sustained or instantaneous, burst. 2   @property cgvector pushdirection  // push distance 3   @property cgfloat magnitude/angle  // angle or thrust size 4   

(6 Uidynamicitembehavior//Power Item Behavior class: A class that corresponds to the above need to add control, which can be used to get speed or something else to change the status quo

1 // say, for example: 2   @property BOOL allowsrotation  // rotation 3   // friction 4   // elastic, Elastic 5   @property cgfloat density  // density

//You can also know whether your behavior is executing or which animation is executed by the following methods

-(void)willmovetoanimator(Uidynamicanimator *) Animator//When the behavior is moved into the animation, or is removed, it will be called to

(7 Uidynamicitembehavior Action property: equivalent to block, as long as the power item behavior is specified, each encounter this behavior will be executed

Four, demo

This demo is more complex, a mixture of animation, gestures, including gravity, adsorption, collision, friction, curve, no rotation, etc. is a comprehensive and strong example, you can first look at the code snippet applied to

1, drag the gesture, should pay attention to judge 3 time

2, click Gesture, drop, here are three to note,

The first one is randomly generating a very square view, and the random number generation is from (0 to the size of the width of our background UIView-1)

The second is that we're going to use our last block as the view[of our tow, which we need to take control of in the adsorption.]

The third is that we should pay attention to the addition of the content of the animation, create a uidunamicanimatorfrom the beginning, and then add the dynamic behavior [ such as collision of gravity and so on in a class that we encapsulate ], and finally add the content is this Step three

3, adsorption behavior, here are three points to note, the use of the block should pay attention to variables, cyclic strong pointer reference, adsorption of the current stroke is constantly changing

4, the animation is entrusted to follow the Uidynamicanimatordelegate protocol

Dynamicanimatordidpause and Dynamicanimatorwillresume Sub-tables to achieve the end of the animation and the beginning of the action to do

(If there is no _animator.delegate = self; it cannot be performed)

5. Notice here that we encapsulate a dropitbehavior class that is specifically designed to encapsulate our behavior,

such as our gravity (Uigravitybehavior), collision (Uicollisionbehavior), dynamic behavior direction (friction density, such as Uidynamicitembehavior)

The second step in manipulating our animation in this class is to add this dynamic behavior to our animations.

6, also our curve class Bezierpathview, in our Viewcontroller operation our curve class, is in our uiview as the background, so need to set the view Customclass as "Bezierpathview" ,

Specifically, we use block construction curves in our adsorption methods to construct a drag curve along with the adsorption process.

Do not introduce here, see Demo Demo:

Demo:http://pan.baidu.com/s/1o69kfum

OC Development _storyboard--block and animations

Related Article

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.