渐变从上到下切割

时间:2013-01-16 15:17:31

标签: iphone ios xcode

这是一个跟进问题 - 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;
}

enter image description here

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

2 个答案:

答案 0 :(得分:1)

它与@Putz1103答案非常相似。你应该从上一个开始创建一个新方法 - (UIImage *)reflectImage:(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];
祝你好运!

答案 1 :(得分: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);

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