【问题标题】:What is the most efficient way to add a reflection to a UIImageView向 UIImageView 添加反射的最有效方法是什么
【发布时间】:2010-01-13 17:24:32
【问题描述】:

我只想用最简单的方法在易于管理的 UIImageVies 下进行反射。

【问题讨论】:

    标签: iphone objective-c reflection uiimageview


    【解决方案1】:

    只需使用sample code in the the iPhone SDK library

    更新:链接现已更新到新位置

    【讨论】:

    • 酷,上次我必须这样做时不存在 :-)
    • 抱歉,网址现在无效
    • 不错。太糟糕了,在 Apple 的示例中执行此操作的代码是一组 3 个方法,而不是拖放类。 (并且它不会为视网膜显示创建正确的比例图像!)。将修复并希望我可以在完成后分享它;)
    【解决方案2】:

    正如 Phil 所说,您可以拥有一个“反射”的 UIImageView 实例:

    @interface ReflectedImageView : UIView 
    {
    @private
        UIImageView *_imageView;
        UIImageView *_imageReflectionView;
    }
    
    @property (nonatomic, retain) UIImage *image;
    
    @end
    

    然后,在你的实现中,像这样

    @implementation ReflectedImageView
    
    @dynamic image;
    
    - (id)initWithFrame:(CGRect)frame 
    {
        if (self = [super initWithFrame:frame]) 
        {
            self.backgroundColor = [UIColor clearColor];
    
            // This should be the size of your image:
            CGRect rect = CGRectMake(0.0, 0.0, 320.0, 290.0);
    
            _imageReflectionView = [[UIImageView alloc] initWithFrame:rect];
            _imageReflectionView.contentMode = UIViewContentModeScaleAspectFit;
            _imageReflectionView.alpha = 0.4;
            _imageReflectionView.transform = CGAffineTransformMake(1, 0, 0, -1, 0, 290.0);
            [self addSubview:_imageReflectionView];
    
            _imageView = [[UIImageView alloc] initWithFrame:rect];
            _imageView.contentMode = UIViewContentModeScaleAspectFit;
            [self addSubview:_imageView];
        }
        return self;
    }
    
    - (void)setImage:(UIImage *)newImage
    {
        _imageView.image = newImage;
        _imageReflectionView.image = newImage;
    }
    
    - (UIImage *)image
    {
        return _imageView.image;
    }
    
    - (void)dealloc 
    {
        [_imageView release];
        [_imageReflectionView release];
        [super dealloc];
    }
    
    @end
    

    【讨论】:

    • 为什么要改变反射图像的 alpha,然后只返回一张图像?
    • 我更改 alpha 以创建一种“金属”反射效果 :) 至于属性 getter,鉴于两个图像视图接收相同的图像,返回其中一个是相同的,并且我选择从 _imageView 返回图像,但这是一个任意选择。感谢您选择我的答案!
    • 这就是大部分的方式。但是,它不包括“淡入淡出”效果。
    【解决方案3】:

    我写了一篇关于如何生成任何 UI 元素或元素组的反射的文章。罐头技术可以放入您的项目中,并且非常易于使用。源码和文章可以在http://aptogo.co.uk/2011/08/no-fuss-reflections/找到

    【讨论】:

      【解决方案4】:

      最简单的方法是在 Photoshop 中手动完成! (说真的 - 如果这很实际,就这样做)。

      否则,您需要制作图像的倒置副本(或至少下半部分)以放置在真实图像下方,并覆盖从黑色渐变(假设您的背景为黑色),alpha=1 到 alpha =0。

      如果您需要将其放置在任意背景上,它会稍微复杂一些,因为您必须将从 alpha = 0 到 alpha = 1 的渐变应用到倒置图像。 我曾经敲过一些代码来做这件事 - 如果没有其他人更早提出任何建议,我会在稍后使用我的 Mac 时尝试挖掘它。

      【讨论】:

      • 呃,OP如何对其他用户的图像进行反射?这个想法是他的应用程序有反射,就像 Cover Flow 一样。
      • 你在那里做了很多假设,@JBRWilkinson
      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2015-03-12
      • 1970-01-01
      • 2016-02-15
      • 2023-04-01
      相关资源
      最近更新 更多