0

这是一个后续问题 -从左到右的梯度方向

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

苹果反射示例

当大小滑块移动时,图像从下到上剪切。移动滑块时如何将其从上到下剪切?我试图更好地理解本教程

//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。你现在看到了我的困境

4

2 回答 2

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];

祝你好运!

于 2013-01-16T17:31:26.813 回答
0

我的猜测是这一行:

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);

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

于 2013-01-16T15:48:41.597 回答