English 中文(简体)
t能够把视像延伸到其束缚的层上角和阴影吗?
原标题:Can t set cornerRadius AND shadow on layer that has an image view stretched to its bounds?

这给我留下了.。 我有《律师意见》(称为“父母”)。 这种观点的最底层是“UIImageView”(称为“儿童”),其范围占“父母”约束的全部。

我想根据“父母”的观点四分五裂,并 set下阴影。 我在“父母”的<代码>:上照常说:

[[parent layer] setShadowOffset:CGSizeMake(5, 5)];
[[parent layer] setShadowRadius:6];
[[parent layer] setShadowOpacity:0.4];    
[[parent layer] setCornerRadius:6];

这表明阴影是正确的,但并没有四舍五入。

The kicker:

  1. If I remove the "child" image view, or shrink it so it doesn t occupy the whole bounds of the "parent" view, I get the rounded corners and shadow correctly on the parent.
  2. If I leave the "child" alone but set "clipsToBounds" on the "parent" view, I get the corners correctly. But now the shadow s gone.
  3. Setting the corner radius on the layer of the child as well seems to have no effect.

似乎像“儿童”形象观点一样,仅仅在“父母”的观念上抹去了四分五裂的角落,因为“父母”的态度是完全的,而根据家长的观念,lipp倒了角落,但也掩盖了阴影。 不清楚第3号为什么不工作。

我失踪了什么? 在这么长的时间里,我看不出一些显而易见的事情吗?

感谢。

(巧合的是,“环绕的玉米-”已经存在。) Awesome.)

最佳回答

你们将需要两种nes的观点,一种是把玉米 setting为一环,而另一种是外部看法(而不是lipp)。 在你本人和外部看来,“儿童”和“父母”很可能是“儿童”的,但我猜测你为这些观点规定了正确的贬低价值?

的答复。 为什么面罩ToBounds = YES防止了CALayer阴影?

问题回答

通常,你必须排出lip。 ToBounds要四舍五入,但由于你想要保留阴影,你还必须走下阴影的角落。 你们是否尝试用警钟的道路铺设阴影? 记账单 ToBounds/masks ToBounds to theault, NO. 类似:

  [[parent layer] setCornerRadius:6.0f];
  [[parent layer] setShadowPath:
             [[UIBezierPath bezierPathWithRoundedRect:[parent bounds] 
                   cornerRadius:6.0f] CGPath]];

您是否尝试确定儿童遗属 这一观点也使世界四分五裂? 也许,它会推翻集装箱观点的阴影。 只是一种想法,不能肯定会奏效。

快速到3,你可以选择以下两种代码之一,以便把玉米和阴影放在图像观点或含有图像层的观点上。


#1. Using UIView, CALayer and Spring and Struts

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // constants
        let radius: CGFloat = 20, dimension: CGFloat = 200, offset = 8
        let frame = CGRect(x: 0, y: 0, width: 200, height: 200)

        // custom view
        let customView = UIView(frame: frame)
        customView.contentMode = .scaleAspectFill

        // image layer
        let imageLayer = CALayer()
        imageLayer.contentsGravity = kCAGravityResizeAspectFill
        imageLayer.contents = UIImage(named: "image")!.cgImage
        imageLayer.masksToBounds = true
        imageLayer.frame = frame
        imageLayer.cornerRadius = radius
        imageLayer.masksToBounds = true

        // rounded layer
        let roundedLayer = CALayer()
        roundedLayer.shadowColor = UIColor.darkGray.cgColor
        roundedLayer.shadowPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: dimension, height: dimension), cornerRadius: radius).cgPath
        roundedLayer.shadowOffset = CGSize(width: offset, height: offset)
        roundedLayer.shadowOpacity = 0.8
        roundedLayer.shadowRadius = 2
        roundedLayer.frame = frame

        // views and layers hierarchy
        customView.layer.addSublayer(imageLayer)
        customView.layer.insertSublayer(roundedLayer, below: imageLayer)
        view.addSubview(customView)

        // layout
        customView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
        customView.autoresizingMask = [UIViewAutoresizing.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
    }

}

#2. Using UIView, UIImageView, CALayer and Auto Layout

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // constants
        let radius: CGFloat = 20, dimension: CGFloat = 200, offset = 8

        // image view
        let imageView = UIImageView(image: UIImage(named: "image"))
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = radius
        imageView.layer.masksToBounds = true

        // rounded view
        let roundedView = UIView()
        roundedView.layer.shadowColor = UIColor.darkGray.cgColor
        roundedView.layer.shadowPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: dimension, height: dimension), cornerRadius: radius).cgPath
        roundedView.layer.shadowOffset = CGSize(width: offset, height: offset)
        roundedView.layer.shadowOpacity = 0.8
        roundedView.layer.shadowRadius = 2

        // views hierarchy
        roundedView.addSubview(imageView)
        view.addSubview(roundedView)

        // layout
        imageView.translatesAutoresizingMaskIntoConstraints = false
        roundedView.translatesAutoresizingMaskIntoConstraints = false
        roundedView.widthAnchor.constraint(equalToConstant: dimension).isActive = true
        roundedView.heightAnchor.constraint(equalToConstant: dimension).isActive = true
        imageView.widthAnchor.constraint(equalTo: roundedView.widthAnchor).isActive = true
        imageView.heightAnchor.constraint(equalTo: roundedView.heightAnchor).isActive = true
        roundedView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        roundedView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        imageView.centerXAnchor.constraint(equalTo: roundedView.centerXAnchor).isActive = true
        imageView.centerYAnchor.constraint(equalTo: roundedView.centerYAnchor).isActive = true
    }

}

两种法警都会产生以下表现:

“entertext/>


你们可以找到更多办法,把图像与圆角和影子结合起来:。 Github repo

如果你想用玉米半径进行图像调查,那么更好的解决办法就把图像调查作为分辨率和1点差。

 imgBrandLogo.backgroundColor = UIColor.blue
    imgBrandLogo.layer.cornerRadius = imgBrandLogo.frame.height/2
    imgBrandLogo.clipsToBounds = true
    viewBrandLogo.layer.shadowColor = UIColor(rgb:0x262626,alpha:0.24).cgColor
    viewBrandLogo.layer.shadowOffset = CGSize(width: 0, height: 1)
    viewBrandLogo.layer.shadowOpacity = 1
    viewBrandLogo.layer.shadowPath = UIBezierPath(roundedRect:imgBrandLogo.bounds , cornerRadius: imgBrandLogo.frame.height/2).cgPath
    viewBrandLogo.backgroundColor = UIColor.clear.withAlphaComponent(0.0)




相关问题
iPhone animation problem SImulator vs Real Device

This is my first question her so please be gentle: I have followig animation code running smoothly on the simulator as well as on the real device (I am testng on iPhone 3GS 3.1.2). Animation is a ...

iPhone animation efficiency solutions, layering questions

I m working on writing a rather basic app. It uses minimal touch input, and mostly just plays fancy animations. There isn t much animation, but the way it s currently done seems super inefficient and ...

iPhone animation

I trying to do an animation on the iPhone and I hope someone can help me out here as I m stumped. I am using CAKeyframeAnimation with CAPath to animate views around. I have these settings set: ...

CALayer filters and bounds

I m just starting writing some Core Animation code and I ve just spent a frustrating day trying to figure out a particular problem. I have two layer-backed views that together make up a wizard/ ...

Animating a shape with CoreAnimation

I approaching core animation and drawing empirically. I am trying to animate a simple shape; the shape in question is formed by 3 lines plus a bezier curve. A red line is also drawn, to show the curve ...

热门标签