我正在使用一种方法为我的按钮添加阴影。问题是在那之后,我无法点击它们。我将userinteractionenabled属性添加到YES,但仍然无法点按。
这是代码。
我该如何解决?我错过了什么吗?
- (UIView*)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andRadius:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity
{
CGRect shadowFrame; // Modify this if needed
shadowFrame.size.width = 0.f;
shadowFrame.size.height = 0.f;
shadowFrame.origin.x = 0.f;
shadowFrame.origin.y = 0.f;
UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];
shadow.userInteractionEnabled = YES; // Modify this if needed
shadow.layer.shadowColor = color.CGColor;
shadow.layer.shadowOffset = shadowOffset;
shadow.layer.shadowRadius = shadowRadius;
shadow.layer.masksToBounds = NO;
shadow.clipsToBounds = NO;
shadow.layer.shadowOpacity = shadowOpacity;
[view.superview insertSubview:shadow belowSubview:view];
[shadow addSubview:view];
return shadow;
}
答案 0 :(得分:1)
我找到了解决方案
在人民的评论之后,我做了一些改变。这是最终的代码
+ (void)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andBlur:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity
{
CGRect shadowFrame = view.frame;
UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];
shadow.backgroundColor = [UIColor redColor];
shadow.userInteractionEnabled = YES; // Modify this if needed
shadow.layer.shadowColor = color.CGColor;
shadow.layer.shadowOffset = shadowOffset;
shadow.layer.shadowRadius = shadowRadius;
shadow.layer.cornerRadius = view.layer.cornerRadius;
shadow.layer.masksToBounds = NO;
shadow.clipsToBounds = NO;
shadow.layer.shadowOpacity = shadowOpacity;
[view.superview insertSubview:shadow belowSubview:view];
}
有了这个,您可以同时拥有圆角和阴影的视图。当然,启用了触摸事件!
答案 1 :(得分:0)
这是有道理的,因为您在按钮上放置了UIView
,这意味着您无法访问该按钮。您已在UserInteractionEnabled
上设置UIView
,但这没用,因为您没有要求UIView响应触摸事件。在这种情况下,我建议在UIView中添加UIGestureRecognizer
,而不是尝试将UIView
放在按钮上。
答案 2 :(得分:0)
CGRect shadowFrame; // Modify this if needed
shadowFrame.size.width = 0.f;
shadowFrame.size.height = 0.f;
shadowFrame.origin.x = 0.f;
shadowFrame.origin.y = 0.f;
UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];
在上面的代码中,您有一个框架0,0,0,0的视图。那么你怎么能点击它?