For reasons outside the capabilities of Cocos2D, I needed to make it transparent and show UIView's behind it.
This works fine except under a certain circumstance. When a sprite's opacity becomes more transparent, it makes everything else in Cocos2D drawn under it become transparent for some reason which makes my UIViews show through.
Shown here on the first image is water tiles, drawn in Cocos2D with a UIView of the mountain and the sky showing under it. The second image is the same thing except in Cocos2D there's a black sprite covering the whole screen at half opacity, which for some reason, makes the UIView under it show through. Where this is most visible in my example is the mountain showing through the water tiles.
My delegate's applicationDidFinishLaunching code is as follows:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8, kEAGLColorFormatRGB565
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setOpenGLView:glView];
glView.opaque = NO;
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
//color/gradient BG
bgLayer = [ColorBGView createGradientWithName:#"darkCave"];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
bgLayer.frame = CGRectMake(0, 0, screenHeight, screenWidth);
[viewController.view.layer insertSublayer:bgLayer atIndex:0];
// add custom parallax view
parallaxView = [[ParallaxView alloc] init];
parallaxView.frame = CGRectMake(0, 0, 1024, 768);
[viewController.view insertSubview:parallaxView atIndex:1];
[parallaxView release];
// make the OpenGLView a child of the view controller
[viewController.view insertSubview:glView atIndex:2];
viewController.view.opaque = YES;
viewController.view.backgroundColor = [UIColor blackColor];
// make the View Controllers childs of the main window
window.rootViewController = viewController;
foregroundLabelView = [[ForegroundLabelView alloc] init];
foregroundLabelView.frame = CGRectMake(0, 0, 1024, 768);
[viewController.view insertSubview:foregroundLabelView atIndex:3];
foregroundLabelView.hidden = YES;
[foregroundLabelView release];
[window makeKeyAndVisible];
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888]; //
[glView setMultipleTouchEnabled:YES];
// Removes the startup flicker
[self removeStartupFlicker];
// Run the intro Scene
[[CCDirector sharedDirector] runWithScene: [BlankScene scene]];
}
Please let me know if you need anything else. Thank you.
Related
The Core Animation Programming Guide led me to believe that a sublayer could, by default, extend outside the bounds of a host view without being clipped. But that's not what's happening for me.
Here's how I initialize my layer-hosting custom view:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self != nil) {
mBaseImage = [[NSImage imageNamed: #"Button.png"] retain];
mSliderImage = [[NSImage imageNamed: #"Track.png"] retain];
mRootLayer = [CALayer layer];
mRootLayer.masksToBounds = NO;
[self setLayer: mRootLayer ];
[self setWantsLayer: YES];
mRootLayer.contents = mBaseImage;
mSliderLayer = [CALayer layer];
[mRootLayer addSublayer: mSliderLayer];
NSSize imSize = [mSliderImage size];
NSRect sliderBounds = NSMakeRect( 0.0f, 0.0f, imSize.width, imSize.height );
mSliderLayer.bounds = sliderBounds;
mSliderLayer.contents = mSliderImage;
mSliderLayer.position = NSMakePoint( 31.0f, 31.0f );
}
return self;
}
The host view, and the image used as its contents, are 62x62, while the sublayer image is 90x10. But the whole thing gets clipped to the 62x62 bounds. What am I missing?
The trick seems to be that the view must be contained in another view (maybe the content view of the window) that has a layer. (Is that documented?)
So I've got a UISlider that I've been working on and I need it to have a custom image. I've got a nice #2x image which has a great amount of pixels and looks great when I add it to a UIImageView, but the moment I use the same image for a replacement for the UISlider thumb image, it pixelates the heck out of it and makes the thumb of the slider look like crap. Any thoughts on how to remedy this?
Here is some of my sample code.
slider = [[UISlider alloc] initWithFrame:CGRectMake(sliderOffset, 29.5, siderW, 50)];
[slider addTarget:self action:#selector(sliderAction) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor];
slider.minimumValue = 0.0;
slider.maximumValue = 10000.0;
slider.continuous = YES;
slider.value = 3000.00;
[slider setThumbImage:[UIImage imageNamed:#"Slider#2x.png"] forState:UIControlStateNormal];
[slider setThumbImage:[UIImage imageNamed:#"Slider#2x.png"] forState:UIControlStateHighlighted];
Thanks in advance!
Here's how I solved it:
1) Subclassed UISlider
2) Created a clear image and set the slider thumb to that clear image.
[self setThumbImage:[UIImage imageNamed:#"ClearImageForOverride.png"] forState:UIControlStateNormal];
[self setThumbImage:[UIImage imageNamed:#"ClearImageForOverride.png"] forState:UIControlStateHighlighted];
3) Created a UIImageView and made that follow the center of the thumb.
float imageWH = PassedInHeightWidthFromSubClass;
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 1, imageWH, imageWH)];
imageView.image = [UIImage imageNamed:#"NewThumbImage"];
[self addSubview:imageView];
[self bringSubviewToFront:imageView];
imageView.center = CGPointMake(thumbRect.origin.x + self.frame.origin.x + localImageViewCenter, self.frame.origin.y - sliderImageViewYOffset);
[self addTarget:self action:#selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
4) Then when the sider value changes I tell the imageView where it's center should be.
-(void)sliderValueChanged {
CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbRect = [self thumbRectForBounds:self.bounds trackRect:trackRect value:self.value];
imageView.center = CGPointMake(thumbRect.origin.x + self.frame.origin.x + localImageViewCenter, self.frame.origin.y - sliderImageViewYOffset);
}
Essentially with this you can have any size of image for the slider knob which looks great and works flawlessly as the knob.
I have this scrollView:
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.scrollView.contentSize = CGSizeMake(320,3000);
_scrollView.frame = CGRectMake(0, 45, 320, 420);
and I want to make it autoscroll very slowly downward to the end so that the user can see the content (as in movie credits), eventually with a button to stop/play, but to follow the user gestures when touching the interface.
How can I do this?
Thanks,
You can use:
[_scrollView setContentOffset:CGPointMake(x,y) animated:YES];
and use the x and y as the touch points on the screen you can capture.
You can also do an animation with CoreAnimation:
[UIScrollView beginAnimations:#"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:1.0f];
[scroll setContentOffset:CGPointMake(x, y)];
[UIScrollView commitAnimations];
this adapted code did the trick (source http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/)
PS : each image is 280 by 200
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
}
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<=10; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"sti%02i.jpg",i]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, ((i-1)*scrMain.frame.size.height+100), 280, 200)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width, scrMain.frame.size.height*10)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(scrollingTimer) userInfo:nil repeats:YES];
}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset ( which page is being displayed )
CGFloat contentOffset = scrMain.contentOffset.y;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
// if page is not 10, display it
if( nextPage!=10 ) {
[scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=nextPage;
// else start sliding form 1 :)
} else {
[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=0;
}
}
You can set x if you want to scroll horizontally, otherwise set y to scroll vertical.
[_scrollView setContentOffset:CGPointMake(x, y) animated:YES];
and modify the co-ordinates accordingly.
I want my NSView to have both gradient background and shadow, so I subclassed NSView and overrode the - (void)awakeFromNib method like this:
[self setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
CAGradientLayer *backgroundGradient = [[CAGradientLayer alloc] init];
backgroundGradient.colors = #[
(__bridge id)CGColorCreateGenericGray(0.85, 1.0),
(__bridge id)CGColorCreateGenericGray(0.94, 1.0)
];
backgroundGradient.masksToBounds = NO;
CALayer *shadowLayer = [CALayer layer];
shadowLayer.shadowColor = CGColorCreateGenericGray(0.5, 1.0);
shadowLayer.shadowOffset = NSMakeSize(0, 0.0);
shadowLayer.shadowRadius = 10.0;
shadowLayer.shadowOpacity = 1.0;
shadowLayer.masksToBounds = NO;
[backgroundGradient addSublayer:shadowLayer];
[self setLayer:backgroundGradient];
However, only the gradient was shown. In addition, if I try [self setLayer:shadowLayer] then the shadow is shown (but of course, no gradient).
How to show both the gradient and the shadow? Thanks a lot!
How to draw a default image in imageview in the center of imageView?using - (void)drawRect:(NSRect)rect overridden method of NSImageView
Yes. That was one way. I've used the following code.
// Drawing
- (void)drawRect:(NSRect)rect
{
if([self image])
{
[[NSColor grayColor] set];
NSRectFill(rect);
//ImageView Bounds and Size
NSRect vBounds = [self bounds];
NSSize vSize = vBounds.size;
//Get the size and origin of default image set to imageView
NSRect imageRect;
imageRect.size = [[self image] size];
imageRect.origin = NSZeroPoint;
//Create a preview image
NSSize previewSize = NSMakeSize( [self image].width / 4.0, [self image].height / 4.0 );
NSImage *previewImage = [[NSImage alloc] initWithSize:previewSize];
//Get the point where the preview image needs to be draw
NSRect newRect;
newRect.origin.x = vSize.width/2-previewSize.width/2;
newRect.origin.y = vSize.height/2-previewSize.height/2;
newRect.size = [previewImage size];
//Draw preview image in imageView
[[self image] drawInRect:newRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1.0];
[previewImage release];
}
}
Why not just set the image view's image initially to the default image, then change it later?