UIView不透明度渐变

时间:2011-02-12 13:09:25

标签: iphone objective-c ipad uikit core-graphics

是否可以使用带有纹理背景图像和不透明度渐变的UIIView?即,bg图像应该从背靠背容器视图从左到右淡入。

感谢

2 个答案:

答案 0 :(得分:0)

使用Alpha通道(即PNG)保存背景图像,并使用您使用的任何图形应用程序淡入淡出。

然后,把它作为你的UIView的第一个孩子作为UIImageView

答案 1 :(得分:0)

- (void)viewDidLoad {
    [super viewDidLoad];
    image1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Default.png"]];
    image1.frame=CGRectMake(0, 0, 320, 460);
    image2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"directoryPage.png"]];
    image2.frame=CGRectMake(0, 0, 320, 480);
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)fade
{   
    image2.alpha = 0;
    [self.view addSubview:image2];
    // The upper layer will transition from alpha 1 to 0s
    [self.view addSubview:image1];
    BSheepAppDelegate *appD=(BSheepAppDelegate *)[[UIApplication sharedApplication]delegate];
    if(appD.flag==0){
        timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(fadeTimerEvent) userInfo:nil repeats:YES];
        appD.flag=1;
    }
    else if (appD.flag==1) {
    [self.view addSubview:image2];
    image2.alpha=1;
    }
}

// Rate of transition for alpha values
#define FADE_IN_RATE        1.0/100.0
#define FADE_OUT_RATE   2.0/200.0
- (void)fadeTimerEvent
{
    if (image2.alpha >= 1)
    {
        // At this point, image2 is now visible  
        [timer invalidate];
        timer = nil;   

    }
    else
    { 
      // Fade lower layer in (increase alpha)
        image2.alpha += FADE_IN_RATE;
        // Fade upper layer out (decrease alpha)
        image1.alpha -= FADE_OUT_RATE;
    }
}

使用此代码设置

相关问题