[英]UIView having Animation from right to left
如何使uiview动画化,使其从右向左缓慢显示,并在其上具有两个UIButton,按钮上有图像,其中一个按钮上的图像将在单击按钮时更改
You can try this code and make changes according to your requirements . 您可以尝试使用此代码并根据需要进行更改。 here i create a baseview which moves from right to left and vice versa.
在这里,我创建了一个从右向左移动,反之亦然的基视图。 In that baseview you put whatever u want to animate.
在该基本视图中,您放置了要设置动画的任何内容。 set bounceDistance,bounceDuration,animateWithDuration,delay variable according to your requirement.
根据您的要求设置bounceDistance,bounceDuration,animateWithDuration,延迟变量。
-(IBAction)rightClicked:(id)sender {
CGRect initalFrame = self.baseView.frame;
initalFrame.origin.x = initalFrame.size.width;
self.baseView.frame = initalFrame;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveFromLeftOrRight:) userInfo:@0 repeats:NO];
}
-(IBAction)leftClicked:(id)sender {
CGRect initalFrame = self.baseView.frame;
initalFrame.origin.x = -initalFrame.size.width;
self.baseView.frame = initalFrame;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveFromLeftOrRight:) userInfo:@1 repeats:NO];
}
-(void)moveFromLeftOrRight:(NSTimer *) timer {
BOOL isLeft = [timer.userInfo boolValue];
CGFloat bounceDistance = 10;
CGFloat bounceDuration = 0.2;
[UIView animateWithDuration:.2 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent
animations:^{
CGFloat direction = (isLeft ? 1 : -1);
self.baseView.center = CGPointMake(self.baseView.frame.size.width/2 + direction*bounceDistance, self.baseView.center.y);}
completion:^(BOOL finished){
[UIView animateWithDuration:bounceDuration animations:^{
self.baseView.center = CGPointMake(self.baseView.frame.size.width/2, self.baseView.center.y);
}];
}];
}
[UIView animateWithDuration:0.7f
animations:^ {
CGRect frame = YourView.frame;
frame.origin.x = 0;
YourView.frame = frame;
YourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
}
completion:^(BOOL finished) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.3];
[UIView commitAnimations];
}];
Assuming you are targeting iPhone. 假设您的目标是iPhone。
Set the view's x co ordinate to, say 320(max visible in iPhone) in the storyboard (IB). 在情节提要(IB)中将视图的x坐标设置为例如320(在iPhone中最大可见)。
x-320 y-0 width-320 height-568
In the viewDidAppear: you can animate and bring the view to the starting co ordinate, say 0. You can do this as follows 在viewDidAppear中:您可以设置动画并将视图带到起始坐标(例如0)。您可以按照以下步骤进行操作
-(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [UIView animateWithDuration:10.f animations:^{ self.view.frame = CGRectMake(0.f, 0.f, 320.f, 568.f); }]; }
For the buttons you will have to be specific. 对于按钮,您必须要明确。
Right to Left animation & Left to right animation as- 从右到左动画和从左到右动画as-
let firstViewTowardsLeft = UIView(frame: CGRect(x: 0, y: 150, width: 50, height: 50))
firstViewTowardsLeft.backgroundColor = .red
view.addSubview(firstViewTowardsLeft)
UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
firstViewTowardsLeft.frame = CGRect(x: 100, y: 150, width: 50, height: 50)
}, completion: nil)
let secondViewTowardsLeft = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
secondViewTowardsLeft.backgroundColor = .red
view.addSubview(secondViewTowardsLeft)
UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
secondViewTowardsLeft.frame = CGRect(x: 0, y: 100, width: 50, height: 50)
}, completion: nil)
https://developer.apple.com/documentation/uikit/uiview/1622418-animate https://developer.apple.com/documentation/uikit/uiview/1622418-animate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.