1, wrote a small demo, to achieve the effect of moving up and down:
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 some small notes that need to be noted, such as the size of your two view, and you need to calculate the exact position.
Let's start with the [UIView Animatewithduration] Method:
Function Prototypes:
+ (void) Animatewithduration: (nstimeinterval) duration animations: (void (^) (void) Animations __osx_available_ Starting (__MAC_NA,__IPHONE_4_0); Delay = 0.0, options = 0, completion = NULL
Duration: Animation duration
Animations: The code block is the definition of all the attributes of all the animations that the view wants to complete;
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__osx_available_starting (__MAC_NA,__IPHONE_4_0);//delay = 0.0, options = 0
Options: is an animation execution option
Completion: Is the code block that you want to execute after the animation completes.