1

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

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

我按照http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html获得了覆盖,但我现在添加了可点击的 UIViews(例如关闭按钮以关闭覆盖)。问题是当我设置 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
4

1 回答 1

0

用 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
}
于 2012-05-31T20:04:38.177 回答