向兼容 iOS 3.0 的 UIView 添加轮廓阴影

发布于 2024-09-29 15:38:06 字数 798 浏览 3 评论 0原文

我有两个 UIViewController A 和 B,它们作为子视图添加到 AppDelegate 中,B 位于 A 之上。

当点击 B 上的 UIButton 时,B 会滑向左侧,代码如下:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(slideOutFinished)];
simonSaysView.view.frame = CGRectMake(40-BView.view.frame.size.width, BView.view.frame.origin.y, BView.view.frame.size.width, BView.view.frame.size.height);
[UIView commitAnimations];

在右侧边缘,我想要一个 20px 的落差阴影,但我无法使用 BView.view.layer.shadow.... 的阴影属性,因为它仅限于 3.2/4.0+。而且动画的表现很糟糕,幻灯片非常滞后(没有阴影很流畅)。

我正在考虑使用自定义 UIView 并在 drawRect 中做一些魔法,但这是否可能,或者我只能在视图的边界内绘制?

希望你们中的一些人可以帮助我。

干杯 对象新资料

I have two UIViewControllers A and B and they are added as subviews in the AppDelegate with B on top of A.

When a UIButton on B is tapped B slides off to the left with the following code:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(slideOutFinished)];
simonSaysView.view.frame = CGRectMake(40-BView.view.frame.size.width, BView.view.frame.origin.y, BView.view.frame.size.width, BView.view.frame.size.height);
[UIView commitAnimations];

On the right edge I want a 20px drop shadow but i cannot use the shadow properties of BView.view.layer.shadow.... because its 3.2/4.0+ only. And the performance of the animation is awful, the slide is lagging extremely much (Its fluent without the shadow).

I'm thinking using a custom UIView and do some magic in drawRect but is this possible or can i only draw within the bounds of the view?

Hope some of you guys and girls can help me.

Cheers
objneodude

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2024-10-06 15:38:06

用下面的代码解决了

// Add drop shadow to the view.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(self.view.frame.size.width, 0, 30, self.view.frame.size.height);
gradientLayer.colors = [NSArray arrayWithObjects:
                        (id)[UIColor blackColor].CGColor,
                        (id)[UIColor clearColor].CGColor,
                        nil];
gradientLayer.startPoint = CGPointMake(-2, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);   
[self.view.layer addSublayer:gradientLayer];

Solved with the following code

// Add drop shadow to the view.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(self.view.frame.size.width, 0, 30, self.view.frame.size.height);
gradientLayer.colors = [NSArray arrayWithObjects:
                        (id)[UIColor blackColor].CGColor,
                        (id)[UIColor clearColor].CGColor,
                        nil];
gradientLayer.startPoint = CGPointMake(-2, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);   
[self.view.layer addSublayer:gradientLayer];
¢蛋碎的人ぎ生 2024-10-06 15:38:06

我遇到了同样的问题,在尝试了各种想法之后,我只是在 UIView 后面添加了一个阴影颜色(灰色)的子视图,我需要有一个阴影,请参阅代码:(包括> 3.2.x/4.x 的代码以及。

    /*Shaded/rounded borders on subviews*/
self.viewOne.layer.cornerRadius = 10;

self.viewOne.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewOne.layer.borderWidth = 0.8;

self.viewTwo.layer.cornerRadius = 10;
self.viewTwo.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewTwo.layer.borderWidth = 0.8;

self.viewThree.layer.cornerRadius = 10;
self.viewThree.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewThree.layer.borderWidth = 0.8;

if (SYSTEM_VERSION_LESS_THAN(@"3.2")) {
    self.shadowOne.layer.cornerRadius = 10;
    self.shadowTwo.layer.cornerRadius = 10;
    self.shadowThree.layer.cornerRadius = 10;
}
else{
    //Use QuartzCore to make shadows etc.
    self.viewOne.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewOne.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewOne.layer.shadowOpacity = 0.6;
    self.viewOne.layer.shouldRasterize = YES;
    self.viewOne.layer.shadowRadius = 2;

    self.viewTwo.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewTwo.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewTwo.layer.shadowOpacity = 0.6;
    self.viewTwo.layer.shouldRasterize = YES;
    self.viewTwo.layer.shadowRadius = 2;

    self.viewThree.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewThree.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewThree.layer.shadowOpacity = 0.6;
    self.viewThree.layer.shouldRasterize = YES;
    self.viewThree.layer.shadowRadius = 2;
}

I had the same problem, after trying various ideas, I just added a subview a shadow colour (Gray) behind the UIView I needed to have a shadow, see code: (Included is the code for >3.2.x/4.x as well.

    /*Shaded/rounded borders on subviews*/
self.viewOne.layer.cornerRadius = 10;

self.viewOne.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewOne.layer.borderWidth = 0.8;

self.viewTwo.layer.cornerRadius = 10;
self.viewTwo.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewTwo.layer.borderWidth = 0.8;

self.viewThree.layer.cornerRadius = 10;
self.viewThree.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewThree.layer.borderWidth = 0.8;

if (SYSTEM_VERSION_LESS_THAN(@"3.2")) {
    self.shadowOne.layer.cornerRadius = 10;
    self.shadowTwo.layer.cornerRadius = 10;
    self.shadowThree.layer.cornerRadius = 10;
}
else{
    //Use QuartzCore to make shadows etc.
    self.viewOne.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewOne.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewOne.layer.shadowOpacity = 0.6;
    self.viewOne.layer.shouldRasterize = YES;
    self.viewOne.layer.shadowRadius = 2;

    self.viewTwo.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewTwo.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewTwo.layer.shadowOpacity = 0.6;
    self.viewTwo.layer.shouldRasterize = YES;
    self.viewTwo.layer.shadowRadius = 2;

    self.viewThree.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.viewThree.layer.shadowOffset = CGSizeMake(2, 2);
    self.viewThree.layer.shadowOpacity = 0.6;
    self.viewThree.layer.shouldRasterize = YES;
    self.viewThree.layer.shadowRadius = 2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文