Reputation: 93
Hi I am developing one application.In that i did the animation for one view to move from left to right and right to left and changing the values for labels contained in that view.But that view is removed when i click left or right button and new view is overriding the old view.So i don't want to overriding.Just i want to add new view.My code is
-(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];
}
}
Just i call the centerAnimation1 method in left nad right button action method.After calling this one,i changed the label values.
Upvotes: 5
Views: 33427
Reputation: 629
Below code is tested in Swift 5.2
In my app, I have 2 labels on click of any of the labels I'm moving/sliding the underline (UIView) from left to right or vice versa in the centre of another label.
label1.setOnClickListener {
UIView.animate(withDuration: 0.5) { [self] in
underlineView.center.x = label1.center.x
}
}
label2.setOnClickListener {
UIView.animate(withDuration: 0.5) { [self] in
underlineView.center.x = label2.center.x
}
}
Upvotes: 0
Reputation: 2901
Using Swift 4.2 and Swift 5
Animate UIView from left to right or vice versa. 0 for Left to right and 1 for Right to left
class AnimationView: UIView {
enum Direction: Int {
case FromLeft = 0
case FromRight = 1
}
@IBInspectable var direction : Int = 0
@IBInspectable var delay :Double = 0.0
@IBInspectable var duration :Double = 0.0
override func layoutSubviews() {
initialSetup()
UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.2, options: .curveEaseIn, animations: {
if let superview = self.superview {
if self.direction == Direction.FromLeft.rawValue {
self.center.x += superview.bounds.width
} else {
self.center.x -= superview.bounds.width
}
}
})
}
func initialSetup() {
if let superview = self.superview {
if direction == Direction.FromLeft.rawValue {
self.center.x -= superview.bounds.width
} else {
self.center.x += superview.bounds.width
}
}
}
}
Upvotes: 1
Reputation: 265
Try the following code, I'm using in my apps:
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];
Upvotes: 0
Reputation: 41
when Button click UIView slide right to left and left to right.
Global Declare 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.
Upvotes: 1
Reputation: 3510
try this
First clik L to 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){ }];
then R To 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*/}];
Upvotes: 22
Reputation:
Use the below code,
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];
And check the below link: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_uiview-animations/
Upvotes: 4