Shadow effects on ImageView in iOS

2019-02-14 16:50发布

I am trying to provide a shadow effect to my Imageview just like in this image.

enter image description here

but the problem which I am facing it is that the shadow is actually visible from the bottom of the Imageview.

Here is the code which I have done to add the shadow. The color and all is still not matching with this one.

          CAGradientLayer *shadow = [CAGradientLayer layer];
          shadow.frame = CGRectMake(-100, 70, perspectiveView.frame.size.width, 
           perspectiveView.frame.size.height - 20);
          shadow.startPoint = CGPointMake(1.0, 0.5);
           shadow.endPoint = CGPointMake(0, 0.5);
           shadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 
           alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];

          shadow.opacity = 1.0f;
          [perspectiveView.layer addSublayer:shadow];

Please provide inputs. Also if the approach is wrong, feel free to guide me to any other approach. Thanks in advance.

Also can anyone suggest how to provide a 3D border just like in the image which provides a slight width to the image view?

4条回答
【Aperson】
2楼-- · 2019-02-14 17:01

Try this,

- (void)setShadow {
    [perspectiveView.layer setShadowOffset:CGSizeMake(-5.0, 5.0)];
    [perspectiveView.layer setShadowRadius:5.0];
    [perspectiveView.layer setShadowOpacity:1.0];

}
查看更多
混吃等死
3楼-- · 2019-02-14 17:04

Below code work i have checked properly.

but Shikhar varshney can u check setShadow method call and perspectiveView is not nil.

- (void)setShadow {
    perspectiveView.layer.shadowColor = [UIColor colorWithWhite:0.0 
       alpha:0.4f] CGColor;
    perspectiveView.layer.shadowOffset = CGSizeMake(0, 1);
    perspectiveView.layer.shadowOpacity = 1;
    perspectiveView.layer.shadowRadius = 1.0;
    perspectiveView.clipsToBounds = NO;
}
查看更多
Evening l夕情丶
4楼-- · 2019-02-14 17:09

you can use CALayer class , the layer can manage your visual aspects like background color, border, shadow and with it you can also manage the geometry of it content like size , transform etc .

visual effect(shadowing)

self.yourView.layer.shadowOffset = CGSizeMake(0, 3);
self.yourView.layer.shadowRadius = 5.0;
self.yourView.layer.shadowColor = [UIColor blackColor].CGColor;
self.yourView.layer.shadowOpacity = 1; 

and you also used it for like self.yourView.layer.frame(Geometry)

more info https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/index.html

查看更多
地球回转人心会变
5楼-- · 2019-02-14 17:12

Perhaps, adding a shadow to the layer seems to work. You may want to try something like this:

//  perspectiveView.transform = CGAffineTransformMakeRotation(-50.0f);   
    perspectiveView.layer.shadowOffset = CGSizeMake(10, 10);
    perspectiveView.layer.shadowRadius = 5.0;
    perspectiveView.layer.shadowOpacity = 0.6;

    perspectiveView.layer.masksToBounds = NO;

You will need to playaround with these values to match your requirements. Hope this helps.

Here is my output:

enter image description here

查看更多
登录 后发表回答