1. I wrote a small Demo to implement up and down movement:
The main code is as follows:
-(IBAction) upChange :( id) sender {
If (typeView = down ){
[Self. downBtn setHidden: NO];
[Self. upBtn setHidden: NO];
[UIView animateWithDuration: 0.5 animations: ^ {
Self. upView. center = CGPointMake (self. upView. center. x, 0 );
Self. downView. center = CGPointMake (self. downView. center. x, (self. upView. frame. size. height + self. downView. frame. size. height)/2 );
}];
TypeView = middle;
} Else if (typeView = middle ){
[Self. downBtn setHidden: NO];
[Self. upBtn setHidden: YES];
[UIView animateWithDuration: 0.5 animations: ^ {
Self. upView. center = CGPointMake (self. upView. center. x,-(self. upView. frame. size. height/2 ));
Self. downView. center = CGPointMake (self. downView. center. x, self. downView. frame. size. height/2 );
}];
TypeView = up;
}
}
-(IBAction) downChange :( id) sender {
If (typeView = middle ){
[Self. downBtn setHidden: YES];
[Self. upBtn setHidden: NO];
[UIView animateWithDuration: 0.5 animations: ^ {
Self. upView. center = CGPointMake (self. upView. center. x, self. upView. frame. size. height/2 );
Self. downView. center = CGPointMake (self. downView. center. x, self. upView. frame. size. height + self. downView. frame. size. height/2 );
}];
TypeView = down;
} Else if (typeView = up)
{
[Self. downBtn setHidden: NO];
[Self. upBtn setHidden: NO];
[UIView animateWithDuration: 0.5 animations: ^ {
Self. upView. center = CGPointMake (self. upView. center. x, 0 );
Self. downView. center = CGPointMake (self. downView. center. x, (self. upView. frame. size. height + self. downView. frame. size. height)/2 );
}];
TypeView = middle;
}
}
This is the main code, and there are also some small details that need attention. For example, you need to calculate the exact location of the two views.
The following describes how to use the [UIView animateWithDuration] method:
Function prototype:
+ (Void) animateWithDuration :( NSTimeInterval) duration animations :( void (^) (void) animations _ notify (_ MAC_NA, __iphone_4_0); // delay = 0.0, options = 0, completion = NULL
Duration: animation duration
Animations: This code block defines all attributes of the animation to be completed for all views;
There are two other functions:
+ (Void) animateWithDuration :( NSTimeInterval) duration delay :( NSTimeInterval) delay options :( UIViewAnimationOptions) options animations :( void (^) (void) animations completion :( void (^) (BOOL finished) completion _ OSX_AVAILABLE_STARTING (_ MAC_NA ,__ IPHONE_4_0 );
+ (Void) animateWithDuration :( NSTimeInterval) duration animations :( void (^) (void) animations completion :( void (^) (BOOL finished) completion _ signature (_ MAC_NA, __iphone_4_0); // delay = 0.0, options = 0
Options: Specifies the animation execution option.
Completion: the code block to be executed after the animation is completed.