好的,经过一段时间的搜索和故障排除后,我仍然卡住了。我尝试动画UIView调整大小,然而,我无法让它为我的生活而努力。我对iOS库和Objective-C都很新,所以这个程序可能不是最有效的。提前谢谢。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {}
-(void)animateBars;
-(void)drawBars;
@end
ViewController.m
#import "ViewController.h"
#import "Background.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
-(void)animateBars
{
CGRect newFrameGreen = CGRectMake(50.0, 0.0, 9.0, self.view.bounds.size.height);
CGRect newFrameOrange = CGRectMake(65.0, 0.0, 9.0, self.view.bounds.size.height);
CGRect newFrameYellow = CGRectMake(80.0, 0.0, 9.0, self.view.bounds.size.height);
[UIView animateWithDuration: 10.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.view viewWithTag:1].frame = newFrameGreen;
[self.view viewWithTag:2].frame = newFrameOrange;
[self.view viewWithTag:3].frame = newFrameYellow;
}
completion:nil];
}
-(void)drawBars
{
UIView *greenBar = [[UIView alloc] initWithFrame:CGRectMake(50.0, 0.0, 9.0, 1.0)];
UIView *orangeBar = [[UIView alloc] initWithFrame:CGRectMake(65.0, 0.0, 9.0, 1.0)];
UIView *yellowBar = [[UIView alloc] initWithFrame:CGRectMake(80.0, 0.0, 9.0, 1.0)];
UIColor *green = [UIColor colorWithRed:142.0/255.0f green:149.0/255.0f blue:31.0/255.0f alpha:1.0f];
UIColor *orange = [UIColor colorWithRed:204.0/255.0f green:82.0/255.0f blue:29.0/255.0f alpha:1.0f];
UIColor *yellow = [UIColor colorWithRed:254.0/255.0f green:190.0/255.0f blue:16.0/255.0f alpha:1.0f];
UIColor *clear = [UIColor colorWithRed:175.0/255.0f green:175.0/255.0f blue:175.0/255.0f alpha:0.0f];
NSArray *greenColors = [NSArray arrayWithObjects:(id)clear.CGColor, green.CGColor, clear.CGColor, nil];
NSArray *orangeColors = [NSArray arrayWithObjects:(id)clear.CGColor, orange.CGColor, clear.CGColor, nil];
NSArray *yellowColors = [NSArray arrayWithObjects:(id)clear.CGColor, yellow.CGColor, clear.CGColor, nil];
CAGradientLayer *greenGradient = [CAGradientLayer layer];
CAGradientLayer *orangeGradient = [CAGradientLayer layer];
CAGradientLayer *yellowGradient = [CAGradientLayer layer];
greenGradient.startPoint = CGPointMake(0.0, 0.5);
greenGradient.endPoint = CGPointMake(1.0, 0.5);
orangeGradient.startPoint = CGPointMake(0.0, 0.5);
orangeGradient.endPoint = CGPointMake(1.0, 0.5);
yellowGradient.startPoint = CGPointMake(0.0, 0.5);
yellowGradient.endPoint = CGPointMake(1.0, 0.5);
greenGradient.colors = greenColors;
orangeGradient.colors = orangeColors;
yellowGradient.colors = yellowColors;
greenGradient.frame = greenBar.bounds;
orangeGradient.frame = orangeBar.bounds;
yellowGradient.frame = yellowBar.bounds;
[greenBar.layer insertSublayer:greenGradient atIndex:0];
[orangeBar.layer insertSublayer:orangeGradient atIndex:0];
[yellowBar.layer insertSublayer:yellowGradient atIndex:0];
greenBar.tag = 1;
orangeBar.tag = 2;
yellowBar.tag = 3;
[self.view addSubview:greenBar];
[self.view addSubview:orangeBar];
[self.view addSubview:yellowBar];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self drawBars];
[self animateBars];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CAGradientLayer *bgLayer = [Background gradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:0)
您的代码工作正常但您正在为这些对象的相同位置设置动画
只需按
更改原始帧即可CGRect newFrameGreen = CGRectMake(50.0, 50.0, 9.0, self.view.bounds.size.height);
CGRect newFrameOrange = CGRectMake(65.0, 50.0, 9.0, self.view.bounds.size.height);
CGRect newFrameYellow = CGRectMake(80.0, 50.0, 9.0, self.view.bounds.size.height);
答案 1 :(得分:0)
视图框架已更改,您可以通过添加以下代码进行测试:
UIView *greenBar = [[UIView alloc] initWithFrame:CGRectMake(50.0, 0.0, 9.0, 1.0)];
UIView *orangeBar = [[UIView alloc] initWithFrame:CGRectMake(65.0, 0.0, 9.0, 1.0)];
UIView *yellowBar = [[UIView alloc] initWithFrame:CGRectMake(80.0, 0.0, 9.0, 1.0)];
//add test code
greenBar.backgroundColor = [UIColor greenColor];
orangeBar.backgroundColor = [UIColor orangeColor];
yellowBar.backgroundColor = [UIColor yellowColor];
您要拉伸的高度是CALayer颜色,而不是框架:
[greenBar.layer insertSublayer:greenGradient atIndex:0];
[orangeBar.layer insertSublayer:orangeGradient atIndex:0];
[yellowBar.layer insertSublayer:yellowGradient atIndex:0];
Sub CALayer无法按帧自动调整大小。 Here is an answer about CAGradientLayer not autoresizing