簡體   English   中英

使用圖像的自定義進度欄

[英]Custom progress bar using image

我正在使用自定義進度欄,該進度條將根據百分比值顯示圖像的一部分。 我的想法是要有兩個圖像,原始圖像和黑白版本。 我正在顯示黑白,然后根據百分比裁剪原始圖像。 運作良好,但自上而下。

我想問一下,有什么方法可以使原始圖像從下到上“增長”?

到目前為止,以下是我的代碼示例:

    UIImageView *imgDefViewGray = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, 60)];
    imgDefViewGray.image = [UIImage imageNamed:@"castle-gray.png"];
    [self addSubview:imgDefViewGray];

    UIImageView *imgDefView = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, 60)];
    imgDefView.image = [UIImage imageNamed:@"castle.png"];

    CGRect rect = CGRectMake(0, 0 , 60, **50**);
    CGImageRef imageRef = CGImageCreateWithImageInRect([imgDefView.image CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageRef];

    imgDefView = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, **50**)];
    imgDefView.image = img;
    [self addSubview:imgDefView];

因此,通過更改Bold (雙星號)上的值,我實現了根據需要裁剪原始圖像的程度,但方向為top-> bottom。 我想從下到上。

提前致謝

經過多次嘗試,我想出了想要的結果。 以下是我對問題的解決方案。 我希望將來會遇到或想要同樣經歷的人都能從中受益。

    UIImageView *imgDefViewGray = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, 60)];
    imgDefViewGray.image = [UIImage imageNamed:@"castle-gray.png"];//60x60 b&w image

    UIImage originalImg = [UIImage imageNamed:@"castle.png"];//60x60 original image
    CGRect rect = CGRectMake(0, originalImg.size.height-scaleDefence), 60 , scaleDefence);//scaleDefence is just a float variable that converts current percentage value to px.

    CGImageRef imageRef = CGImageCreateWithImageInRect([originalImg CGImage], rect);
    UIImage *croppedImg = [UIImage imageWithCGImage:imageRef];

    UIImageView *imgDefView = [[UIImageView alloc] initWithFrame:CGRectMake(0, originalImg.size.height-scaleDefence, 60, scaleDefence)];//dynamically changing y-axis depending on cropped image's height.
    imgDefView.image = croppedImg;

    [imgDefViewGray addSubview:imgDefView];//make original image part of b&w
    [self addSubview:imgDefViewGray];//finally add the result to the view

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM