如何将uiview从左向右移动,反之亦然

嗨,我正在开发一个应用程序。我做了一个视图的animation从左到右,从右到左移动,并更改该视图中包含的标签的值。但是,当我点击左键或右键时删除该视图和新视图重写旧视图。所以我不想重写。只是我想添加新的视图。我的代码是

-(void)centerAnimation1:(id)sender { UIView *currentView = daysview; theWindow = daysview; // set up an animation for the transition between the views CATransition *animation = [CATransition animation]; animation.delegate = self; [animation setDuration:0.4]; [animation setType:kCATransitionMoveIn]; if(rhtolft) { [animation setSubtype:kCATransitionFromLeft]; } else { [animation setSubtype:kCATransitionFromRight]; } [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"]; [currentView removeFromSuperview]; } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ if(animate) { CATransition *animation = [CATransition animation]; animation.delegate = self; [animation setDuration:0.4]; [animation setType:kCATransitionMoveIn]; if(rhtolft) { [animation setSubtype:kCATransitionFromLeft]; rhtolft=NO; } else { [animation setSubtype:kCATransitionFromRight]; } [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"]; animate=NO; [self.view addSubview:daysview]; } } 

只是我在leftnad右键操作方法中调用了centerAnimation1方法。调用这个之后,我改变了标签值。

尝试这个

先从L到R

  CGRect basketTopFrame = Yourview.frame; basketTopFrame.origin.x = 115; [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = basketTopFrame; } completion:^(BOOL finished){ }]; 

然后R到L

  CGRect napkinBottomFrame = Yourview.frame; napkinBottomFrame.origin.x = 0; [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationOptionCurveEaseOut animations:^{ Yourview.frame = napkinBottomFrame; } completion:^(BOOL finished){/*done*/}]; 

使用下面的代码,

 UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(20.0f, 100.0f, 300.0f, 200.0f)]; [testView setBackgroundColor:[UIColor blueColor]]; [self.view addSubview:testView]; [UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ [testView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)]; } completion:nil]; [testView release]; 

并检查下面的链接: http : //mobile.tutsplus.com/tutorials/iphone/ios-sdk_uiview-animations/

当button点击UIView从右到左和从左到右滑动。

全局声明NSString * string = @“one”;

 - (void)viewDidLoad { [super viewDidLoad]; stringslider = @"one"; } Action Button "click" -(IBAction)slider:(id)sender{ if ([stringslider isEqualToString:@"one"]) { [UIView animateWithDuration:0.2 animations:^{[sliderview setFrame:CGRectMake(205, [sliderview frame].origin.y, 116, 275)]; } completion:nil]; stringslider = @"two"; } else if ([stringslider isEqualToString:@"two"]) { [UIView animateWithDuration:0.2 animations:^{[sliderview setFrame:CGRectMake(342, [sliderview frame].origin.y, 116, 275)]; } completion:nil]; stringslider = @"one"; } } easy uiview animation is done. 

试试下面的代码,我在我的应用程序中使用:

 CGRect rectLeft = btnLeft.frame; rectLeft.origin.x = rectLeft.origin.x + rectLeft.size.width; CGRect rectRight = btnRight.frame; rectRight.origin.x = rectRight.origin.x - rectRight.size.width; [UIView animateWithDuration:0.5 animations:^{ btnLeft.frame = rectLeft; btnRight.frame = rectRight; } completion:nil];