他们给了我一个示例应用程序,截图可以找到 here
我已经读过UIPopoverController是最糟糕的事情,因为只有几个属性可以设置.
LINK& LINK
我尝试降低子视图的Alpha,但是视图的内容也变得透明.
我错过了一个选项,或做错了什么来做到这一点?
此外,我对子类化的经验很少,所以任何建议,如果那样的情况将不仅仅是赞赏.
-碧玉
解决方法 我为你创建了一个UIVIEw的子类,它应该适用于你需要的东西……PopoverVIEw.h:
//// PopoverVIEw.h// popovertest//// Created by Richard Ross III on 12/7/11.// copyright (c) 2011 ultimate Computer Services Inc. All rights reserved.//#import <UIKit/UIKit.h>#import <CoreGraphics/CoreGraphics.h>@interface PopoverVIEw : UIVIEw { UIcolor *backgroundcolor; CGSize contentSize; UIVIEwController *contentVIEwController; CGSize borders;}@property(nonatomic,retain) UIcolor *backgroundcolor;@property(Readonly,assign) CGSize contentSize;@property(nonatomic,retain) UIVIEwController *contentVIEwController;@property(nonatomic,assign) CGSize borders;@end
PopoverVIEw.m
//// PopoverVIEw.m// popovertest//// Created by Richard Ross III on 12/7/11.// copyright (c) 2011 ultimate Computer Services Inc. All rights reserved.//#import "PopoverVIEw.h"@implementation PopoverVIEw@synthesize backgroundcolor,borders,contentSize,contentVIEwController;-(voID) observeValueForKeyPath:(Nsstring *)keyPath ofObject:(ID)object change:(NSDictionary *)change context:(voID *)context{ if ([keyPath isEqualToString:@"frame"] || [keyPath isEqualToString:@"borders"]) { contentSize = CGRectInset(self.frame,borders.wIDth,borders.height).size; contentVIEwController.vIEw.frame = CGRectMake(borders.wIDth,borders.height,contentSize.wIDth,contentSize.height); }}- (ID)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code self.opaque = NO; self.clipsToBounds = NO; self.backgroundcolor = [UIcolor colorWithRed:50/256 green:50/256 blue:50/256 Alpha:0.75f]; self.borders = CGSizeMake(25,25); contentSize = CGRectInset(frame,borders.wIDth * 2,borders.height * 2).size; self.userInteractionEnabled = YES; [self addobserver:self forKeyPath:@"frame" options:kNilOptions context:nil]; [self addobserver:self forKeyPath:@"borders" options:kNilOptions context:nil]; } return self;}-(voID) setContentVIEwController:(UIVIEwController *)contentVIEwController_{ if (contentVIEwController_ != self->contentVIEwController) { [self->contentVIEwController.vIEw removeFromSupervIEw]; self->contentVIEwController = contentVIEwController_; [self addSubvIEw:contentVIEwController_.vIEw]; contentVIEwController_.vIEw.frame = CGRectMake(borders.wIDth,contentSize.height); }}voID CGContextDrawRoundedRect(CGContextRef context,CGcolorRef color,CGRect bounds,CGfloat radius);voID CGContextDrawRoundedRect(CGContextRef context,CGfloat radius){ CGContextSetFillcolorWithcolor(context,color); // If you were making this as a routine,you would probably accept a rectangle // that defines its bounds,and a radius reflecting the "rounded-ness" of the rectangle. CGRect rrect = bounds; // NOTE: At this point you may want to verify that your radius is no more than half // the wIDth and height of your rectangle,as this technique degenerates for those cases. // In order to draw a rounded rectangle,we will take advantage of the fact that // CGContextAddArctopoint will draw straight lines past the start and end of the arc // in order to create the path from the current position and the destination position. // In order to create the 4 arcs correctly,we need to kNow the min,mID and max positions // on the x and y lengths of the given rectangle. CGfloat minx = CGRectGetMinX(rrect),mIDx = CGRectGetMIDX(rrect),maxx = CGRectGetMaxX(rrect); CGfloat miny = CGRectGetMinY(rrect),mIDy = CGRectGetMIDY(rrect),maxy = CGRectGetMaxY(rrect); // Next,we will go around the rectangle in the order given by the figure below. // minx mIDx maxx // miny 2 3 4 // mIDy 1 9 5 // maxy 8 7 6 // Which gives us a coincIDent start and end point,which is incIDental to this technique,but still doesn't // form a closed path,so we still need to close the path to connect the ends correctly. // Thus we start by moving to point 1,then adding arcs through each pair of points that follows. // You Could use a similar tecgnique to create any shape with rounded corners. // Start at 1 CGContextMovetoPoint(context,minx,mIDy); // Add an arc through 2 to 3 CGContextAddArctopoint(context,miny,mIDx,radius); // Add an arc through 4 to 5 CGContextAddArctopoint(context,maxx,mIDy,radius); // Add an arc through 6 to 7 CGContextAddArctopoint(context,maxy,radius); // Add an arc through 8 to 9 CGContextAddArctopoint(context,radius); // Close the path CGContextClosePath(context); // Fill the path CGContextDrawPath(context,kCGPathFill); }- (voID)drawRect:(CGRect)rect{ CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGcolorRef color = [backgroundcolor CGcolor]; CGContextDrawRoundedRect(currentContext,color,CGRectMake(0,self.bounds.size.wIDth,self.bounds.size.height),10);}@end
ArrowVIEw.h:
//// ArrowVIEw.h// popovertest//// Created by Richard Ross III on 12/7/11.// copyright (c) 2011 ultimate Computer Services Inc. All rights reserved.//#import <UIKit/UIKit.h>@interface ArrowVIEw : UIVIEw{ UIcolor *arrowcolor;}@property(nonatomic,retain) UIcolor *arrowcolor;@end
ArrowVIEw.m:
//// ArrowVIEw.m// popovertest//// Created by Richard Ross III on 12/7/11.// copyright (c) 2011 ultimate Computer Services Inc. All rights reserved.//#import "ArrowVIEw.h"#define CGRectCenter(rect) CGPointMake(CGRectGetMIDX(rect),CGRectGetMIDY(rect))#define CGSizediv(size,div) CGSizeMake(size.wIDth / div,size.height / div)@implementation ArrowVIEw@synthesize arrowcolor;- (ID)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code self.opaque = NO; } return self;}// Only overrIDe drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (voID)drawRect:(CGRect)rect{ // Drawing code CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextSetFillcolorWithcolor(currentContext,arrowcolor.CGcolor); CGPoint arrowLocation = CGRectCenter(self.bounds); CGSize arrowSize = CGSizediv(self.frame.size,1); CGPoint arrowTip = CGPointMake(arrowLocation.x,arrowLocation.y + (arrowSize.height / 2)); CGPoint arrowleftFoot = CGPointMake(arrowLocation.x - (arrowSize.wIDth / 2),arrowLocation.y - (arrowSize.height / 2)); CGPoint arrowRightFoot = CGPointMake(arrowLocation.x + (arrowSize.wIDth / 2),arrowLocation.y - (arrowSize.height / 2)); // Now we draw the triangle CGContextMovetoPoint(currentContext,arrowTip.x,arrowTip.y); CGContextAddlinetoPoint(currentContext,arrowleftFoot.x,arrowleftFoot.y); CGContextAddlinetoPoint(currentContext,arrowRightFoot.x,arrowRightFoot.y); CGContextClosePath(currentContext); CGContextDrawPath(currentContext,kCGPathFill);}@end
PopoverVIEwController.h:
//// PopoverVIEwController.h// popovertest//// Created by Richard Ross III on 12/7/11.// copyright (c) 2011 ultimate Computer Services Inc. All rights reserved.//#import <UIKit/UIKit.h>#import "PopoverVIEw.h"#import "ArrowVIEw.h"@interface PopoverVIEwController : UIVIEwController{ PopoverVIEw *popover; ArrowVIEw *arrow; UIPopoverArrowDirection arrowDirection; UIVIEwController *parentVIEwController;}// for managing the content@property(Readonly,retain) PopoverVIEw *popover;// the arrow@property(Readonly,retain) ArrowVIEw *arrow;@property(nonatomic,assign) UIPopoverArrowDirection arrowDirection;-(voID) presentModallyInVIEwController:(UIVIEwController *) controller animated:(BOol) animated;-(voID) dismissModallyFromVIEwController:(UIVIEwController *) controller animated:(BOol) animated;-(voID) dismiss;@end
PopoverVIEwController.m:
//// PopoverVIEwController.m// popovertest//// Created by Richard Ross III on 12/7/11.// copyright (c) 2011 ultimate Computer Services Inc. All rights reserved.//#import "PopoverVIEwController.h"#define degreestoradians(x) (M_PI * x / 180)@implementation PopoverVIEwController@synthesize arrowDirection,arrow,popover;-(voID) updateArrowposition{ if (arrowDirection & UIPopoverArrowDirectionUp) { arrow.frame = CGRectMake((popover.frame.origin.x) + (popover.frame.size.wIDth / 2) - (arrow.frame.size.wIDth / 2),popover.frame.origin.y - (arrow.frame.size.height),arrow.frame.size.wIDth,arrow.frame.size.height); arrow.transform = CGAffinetransformMakeRotation(degreestoradians(180)); } else if (arrowDirection & UIPopoverArrowDirectionleft) { arrow.frame = CGRectMake((popover.frame.origin.x) - (arrow.frame.size.wIDth),(popover.frame.origin.y) + (popover.frame.size.height / 2) - (arrow.frame.size.height / 2),arrow.frame.size.height); arrow.transform = CGAffinetransformMakeRotation(degreestoradians(90)); } else if (arrowDirection & UIPopoverArrowDirectionRight) { arrow.frame = CGRectMake((popover.frame.origin.x) + (popover.frame.size.wIDth),(popover.frame.origin.y) + (popover.frame.size.height / 2) - (arrow.frame.size.height / 2),arrow.frame.size.height); arrow.transform = CGAffinetransformMakeRotation(degreestoradians(-90)); } else if (arrowDirection & UIPopoverArrowDirectionDown) { arrow.frame = CGRectMake((popover.frame.origin.x) + (popover.frame.size.wIDth / 2) - (arrow.frame.size.wIDth / 2),popover.frame.origin.y + popover.frame.size.height,arrow.frame.size.height); } else { NSLog(@"unkNown direction %i",arrowDirection); }}-(voID) observeValueForKeyPath:(Nsstring *)keyPath ofObject:(ID)object change:(NSDictionary *)change context:(voID *)context{ [self updateArrowposition];}-(voID) dismiss{ [self dismissModallyFromVIEwController:parentVIEwController animated:YES];}-(voID) presentModallyInVIEwController:(UIVIEwController *)controller animated:(BOol)animated{ [controller.vIEw addSubvIEw:self.vIEw]; if (animated) { self.vIEw.Alpha = 0.0f; [UIVIEw beginAnimations:@"Modal Entrance" context:nil]; [UIVIEw setAnimationDuration:0.25]; self.vIEw.Alpha = 1.0f; [UIVIEw commitAnimations]; }}-(voID) dismissModallyFromVIEwController:(UIVIEwController *)controller animated:(BOol)animated{ if (animated) { [UIVIEw animateWithDuration:0.25 animations:^{ self.vIEw.Alpha = 0.0f; } completion:^(BOol finished) { [controller dismissModalVIEwControllerAnimated:NO]; }]; } else { [controller dismissModalVIEwControllerAnimated:NO]; }}-(ID) init{ if (self = [super init]) { popover = [PopoverVIEw new]; arrow = [ArrowVIEw new]; popover.backgroundcolor = [UIcolor colorWithRed:50/255 green:50/255 blue:50/255 Alpha:0.75]; arrow.arrowcolor = [UIcolor colorWithRed:50/255 green:50/255 blue:50/255 Alpha:0.75]; arrow.frame = CGRectMake(0,20,20); [self addobserver:self forKeyPath:@"arrowDirection" options:kNilOptions context:nil]; [popover addobserver:self forKeyPath:@"frame" options:kNilOptions context:nil]; [self.vIEw addSubvIEw:popover]; [self.vIEw addSubvIEw:arrow]; arrowDirection = UIPopoverArrowDirectionRight; } return self;}-(voID) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self dismiss];}@end
用法示例:
PopoverVIEwController *popController = [[PopoverVIEwController alloc] init];UIbutton *contentVIEw = [UIbutton buttonWithType:UIbuttonTypeRoundedRect];[contentVIEw setTitle:@"Hello World!" forState:UIControlStatenormal];UIVIEwController *controller = [[UIVIEwController alloc] init];controller.vIEw = contentVIEw;popController.popover.contentVIEwController = controller;popController.popover.frame = CGRectMake(100,100,300,400);[popController presentModallyInVIEwController:self animated:YES];
请注意,这是在启用ARC的情况下完成的,因此如果您使用引用计数,则此代码需要进行一些调整.
为方便起见,我将整个项目放在我的DropBox中,供您下载:
http://dl.dropbox.com/u/25258177/personal/popovertest.zip
以上是内存溢出为你收集整理的iOS UIPopoverController透明度/ alpha全部内容,希望文章能够帮你解决iOS UIPopoverController透明度/ alpha所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)