2012-05-18 163 views
1

想知道是否有人可以闡明如何在ipad或iphone應用程序上創建半透明幫助屏幕?類似於谷歌應用程序。ios幫助屏幕半透明覆蓋

我有一個splitViewController,我想疊加一個透視黑色疊加在splitViewController的頂部,我可以顯示有關每個項目的幫助信息,您可以在splitViewController中單擊。

我跟着http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html這讓我覆蓋,但我現在添加可點擊的UIViews(例如關閉按鈕來消除覆蓋)。問題是當我設置touchesEnded:withEvent我似乎無法將來自主要加載視圖的點擊轉換爲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 
+0

如何簡單地添加透明度的子視圖? –

+0

我正在添加一個帶有透明度的子視圖。看代碼。 – jdog

回答

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 
} 
+0

我很想聽聽你如何用NSNotifications解決這個問題。我處於類似的情況。 –

+0

請參閱上面的編輯到我的答案。 – jdog