gpt4 book ai didi

objective-c - ios帮助屏幕半透明覆盖

转载 作者:行者123 更新时间:2023-11-28 17:33:07 27 4
gpt4 key购买 nike

想知道是否有人可以阐明如何在 iPad 或 iPhone 应用程序上创建半透明的帮助屏幕?类似于谷歌应用程序的东西。

我有一个 splitViewController,我想在 splitViewController 的顶部覆盖一个透明的黑色覆盖层,在那里我可以显示有关您可以在 splitViewController 中单击的每个项目的帮助信息。

我关注了http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html这让我获得了叠加层,但我现在正在添加可点击的 UIView(例如关闭按钮以关闭叠加层)。问题是当我设置 touchesEnded:withEvent 时,我似乎无法将点击从主 LoadingView 转换为 closeButton 以查看实际点击了什么?

查看代码末尾结束的触摸

============================================= =================

#import "HelpView.h"
#import <QuartzCore/QuartzCore.h>

//

CGPathRef NewPathWithRoundRect(CGRect rect, CGFloat cornerRadius)
{
//
// Create the boundary path
//
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL,
rect.origin.x,
rect.origin.y + rect.size.height - cornerRadius);

// Top left corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x,
rect.origin.y,
rect.origin.x + rect.size.width,
rect.origin.y,
cornerRadius);

// Top right corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x + rect.size.width,
rect.origin.y,
rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height,
cornerRadius);

// Bottom right corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height,
rect.origin.x,
rect.origin.y + rect.size.height,
cornerRadius);

// Bottom left corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x,
rect.origin.y + rect.size.height,
rect.origin.x,
rect.origin.y,
cornerRadius);

// Close the path at the rounded rect
CGPathCloseSubpath(path);

return path;
}

@implementation HelpView

@synthesize closeButton = _closeButton;

- (id)loadingViewInView:(UIView *)aSuperview
{


HelpView *loadingView =
[[HelpView alloc] initWithFrame:[aSuperview bounds]];
if (!loadingView)
{
return nil;
}

loadingView.opaque = NO;
loadingView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[aSuperview addSubview:loadingView];

const CGFloat DEFAULT_LABEL_WIDTH = 100.0;
const CGFloat DEFAULT_LABEL_HEIGHT = 40.0;
CGRect labelFrame = CGRectMake(0, 0, DEFAULT_LABEL_WIDTH, DEFAULT_LABEL_HEIGHT);
UILabel *loadingLabel = [[UILabel alloc] initWithFrame:labelFrame];
loadingLabel.text = NSLocalizedString(@"Close", nil);
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.textAlignment = UITextAlignmentCenter;
loadingLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
loadingLabel.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin;


// Background for Close button
CALayer *closeButtonBackground = [CALayer layer];
closeButtonBackground.opacity = 0.3;
closeButtonBackground.borderColor = [UIColor whiteColor].CGColor;
closeButtonBackground.borderWidth = 2.0;
closeButtonBackground.cornerRadius = 5.0;
closeButtonBackground.backgroundColor = [UIColor blackColor].CGColor;

// Finish setting up frame for Close button
labelFrame.origin.x = 0;
labelFrame.origin.y = 0;
loadingLabel.frame = labelFrame;
closeButtonBackground.frame = labelFrame;



CGRect viewRect = CGRectMake(loadingView.frame.size.width -150 ,loadingView.frame.size.height - 85,100,40);
self.closeButton = [[UIView alloc] initWithFrame:viewRect];
self.closeButton.userInteractionEnabled = YES;
self.userInteractionEnabled = NO;
[loadingView addSubview:self.closeButton];
[self.closeButton.layer addSublayer:closeButtonBackground];
[self.closeButton addSubview:loadingLabel];


// Set up the fade-in animation
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[[aSuperview layer] addAnimation:animation forKey:@"layerAnimation"];

//[loadingView createGestureRecognizer];

return loadingView;
}




- (void)removeView
{
UIView *aSuperview = [self superview];
[super removeFromSuperview];

// Set up the animation
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];

[[aSuperview layer] addAnimation:animation forKey:@"layerAnimation"];
}


- (void)drawRect:(CGRect)rect
{
rect.size.height -= 1;
rect.size.width -= 1;

const CGFloat RECT_PADDING = 0.0;
rect = CGRectInset(rect, RECT_PADDING, RECT_PADDING);

const CGFloat ROUND_RECT_CORNER_RADIUS = 0.0;
CGPathRef roundRectPath = NewPathWithRoundRect(rect, ROUND_RECT_CORNER_RADIUS);

CGContextRef context = UIGraphicsGetCurrentContext();

const CGFloat BACKGROUND_OPACITY = 0.85;
CGContextSetRGBFillColor(context, 0, 0, 0, BACKGROUND_OPACITY);
CGContextAddPath(context, roundRectPath);
CGContextFillPath(context);

const CGFloat STROKE_OPACITY = 0.25;
CGContextSetRGBStrokeColor(context, 1, 1, 1, STROKE_OPACITY);
CGContextAddPath(context, roundRectPath);
CGContextStrokePath(context);

CGPathRelease(roundRectPath);
}

- (void)handleSingleDoubleTap:(UITapGestureRecognizer *)sender {
NSLog(@"");
CGPoint tapPoint = [sender locationInView:sender.view];
CALayer* layerThatWasTapped = [self.layer hitTest:tapPoint];

CGPoint convertedPoint = [layerThatWasTapped convertPoint:layerThatWasTapped.position toLayer:self.closeButton.layer];
BOOL myBool = [self.closeButton.layer containsPoint:convertedPoint];


[UIView beginAnimations:nil context:NULL];

sender.view.center = tapPoint;

[UIView commitAnimations];
}

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: self];
CGPoint convertedPoint = [self convertPoint:point toView:[self.closeButton superview]];

if (CGRectContainsPoint(self.closeButton.bounds, convertedPoint)) {
NSLog (@"YAY!");
}

} // touchesEnded

最佳答案

通过 NSNotifications 解决了这个问题。像这样的……它有点静态……应该更动态。

 [[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(eventHandler:)
name:@"SomeButtonClicked"
object:nil ];

-(void)eventHandler: (NSNotification *) notification
{
NSString *buttonWhichWasClicked = [[notification userInfo] objectForKey:@"buttonName"];
NSLog(@" %@", buttonWhichWasClicked);

if ([buttonWhichWasClicked isEqualToString:@"close"]){
[self removeView];
}
//Tell other buttons to dim/remove themselves as a new one was selected
}

- (void)removeView
{
//code to remove view
}

关于objective-c - ios帮助屏幕半透明覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10654722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com