【问题标题】:gradient cut from top to bottom从上到下渐变
【发布时间】:2013-01-16 15:17:30
【问题描述】:

这是一个后续问题 - gradient direction from left to right

在这个苹果反射示例代码中,

Apple Reflection Example

当尺寸滑块移动时,图像从下到上被剪切。移动滑块时如何将其从上到下剪切?我正在尝试更好地理解本教程

//I know the code is in this section here but I can't figure out what to change
- (UIImage *)reflectedImage:(UIImageView *)fromImage withHeight:(NSUInteger)height
{
...
}

    //it probably has something to do with this code. 
//I think this tells it how much to cut. 
//Though I can't figure out how does it know where the 0,0 of the image is and why 
// keep 0,0 of the image on the top? I am assuming this is where it hinges its 
//point and cuts the image from bottom to top
CGContextRef MyCreateBitmapContext(int pixelsWide, int pixelsHigh)
{

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create the bitmap context
    CGContextRef bitmapContext = CGBitmapContextCreate (NULL, pixelsWide, pixelsHigh, 8, 0, colorSpace,(kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
    CGColorSpaceRelease(colorSpace);

    return bitmapContext;
}

如果要反映的图像位于顶部会怎样。所以为了正确地展示它,我需要从上到下展示它,而不是自下而上。那就是我想要达到的效果。在这种情况下,我只是在他们的故事板示例中移动了 UIImageViews。你现在看到了我的困境

【问题讨论】:

    标签: iphone ios xcode


    【解决方案1】:

    这与@Putz1103 的答案非常相似。您应该从上一个 - (UIImage *)reflectedImage:(UIImageView *)fromImage withWidth:(NSUInteger)width 开始创建一个新方法。

    - (UIImage *)reflectedImage:(UIImageView *)fromImage withWidth:(NSUInteger)width andHeight:(NSUInteger)height
    {
         ....
         CGContextClipToMask(mainViewContentContext, CGRectMake(0.0, 0.0, width, height), gradientMaskImage);
         ....
    }
    

    然后在 slideAction 方法中,使用类似:

    self.reflectionView.image = [self reflectedImage:self.imageView withWidth:self.imageView.bounds.size.width andHeight:reflectionHeight];
    

    祝你好运!

    【讨论】:

    • 再次感谢您帮助我。不幸的是,这不起作用。如果您注意到图像的渐变仍在底部。我想要做的是让它翻转另一种方式。不幸的是,你给我的代码并没有这样做。我的意思是我看不出行为上有什么不同。
    • 我想要陶醉的图像在顶部。在我的帖子中查看屏​​幕截图和一些解释。
    • Luis - 你看到顶部的深色渐变了吗?当您移动大小滑块时,渐变会保留在倒置图像的顶部吗?我刚刚清理并再次下载了本教程的新副本,进行了您显示的两个更改,但我仍然看到它的行为方式相同。可以发一张你看到的照片吗?
    • @SamBudda 我将答案限制为:“移动滑块时如何从上到下剪切它?”,这对我有用。您还需要更改渐变的行为吗?
    【解决方案2】:

    我的猜测是这一行:

    CGContextClipToMask(mainViewContentContext, CGRectMake(0.0, 0.0, fromImage.bounds.size.width, height), gradientMaskImage);
    

    如果你把它改成这个,它应该做相反的事情:

    CGContextClipToMask(mainViewContentContext, CGRectMake(0.0, fromImage.bounds.size.height - height, fromImage.bounds.size.width, height), gradientMaskImage);
    

    基本上,您需要将剪切矩形设置为图像的底部,而不是图像的顶部。这将反转您的滑块的作用,但这很容易解决,我会留给您练习。

    【讨论】:

    • 我刚刚尝试过,但没有成功。它仍然从下到上切割
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    相关资源
    最近更新 更多