I write a window and two views,
set background color of window to blue,
and draw a image in view1,
and fill view2 with a color with alpha,like this:
//AppDelegate
[self.window setLevel:(NSStatusWindowLevel + 2)];
TestView *view1 = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
TestView2 *view2 = [[TestView2 alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
[self.window setContentView:view1];
[self.window.contentView addSubview:view2];
//View1
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
NSImage *image = [NSImage imageNamed:#"testImage"];
[image drawInRect:dirtyRect];
}
//view2
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
[[[NSColor redColor] colorWithAlphaComponent:0.5] set];
NSRectFill(dirtyRect);
}
But when the app run, I found I the backgroundcolor of window and image of View1 did not show, only the view2 show, and it is transparent,through it can see the desktop.
If I did not add subview view2, the app runs correctly, the blue background color and the image.
Does anybody know why, really thanks for the help, I will wait on line.
I change the NSRectFill to
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
Then it turns right.
Related
I am writing an app, where I programmatically display a background image and then update some text over the top.
On some event (e.g. a tap), I want to change the text and background image.
I am able to change the text successfully, but the image, is not changing.
I setup the image with the following code, where view, images1 and images2 are:
UIImageView* view;
UIImage* images1;
UIImage* images2;
view = [[UIImageView alloc] initWithImage: image1];
view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
[view setUserInteractionEnabled:YES];
[self.view addSubview:view];
[self.view setContentMode:UIViewContentModeScaleToFill];
[self.view sendSubviewToBack:view];
and then I try to change the background with:
view.image = image2;
but the image does not change, any ideas?
Try this code to change the image on tap gesture
First you have to define UIImageView in header file like this.
#interface ChangeImageViewController ()
UIImageView *Imgview;
#end
#implementation ChangeImageViewController
Imgview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
Imgview.image=[UIImage imageNamed:#"firstImageName.png"];
[Imgview setUserInteractionEnabled:YES];
[self.view addSubview:Imgview];
[self.view sendSubviewToBack:Imgview];
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(TaponImgview:)];
tapGesture.numberOfTapsRequired=1;
[Imgview addGestureRecognizer:tapGesture];
And In Tap Gesture Recognizer Method.
-(void)TaponImgview:(UITapGestureRecognizer *)recognizer
{
Imgview.image=[UIImage imageNamed:#"secondImage.png"]; // Your second image that you want to change it
}
And So finally your image will change on tap on UIImageview..Thanks
I'm trying to draw some things on the background of my windows. Therefore I subclassed the NSView of the window and added some drawing code like this:
- (void)drawRect:(NSRect)dirtyRect {
float color = 0.95;
[[NSColor colorWithDeviceRed:color green:color blue:color alpha:1.0] set];
NSRectFill(NSMakeRect(320, 0, 220, NSHeight(dirtyRect)-60));
}
This works great, but as soon as I open a NSComboBox or if I activate a checkbox, the background of these elements erases my just drawn rect.
I don't understand this, because checking for example the checkbox causes, that drawRect is called (I added a NSLog). Only resizing the window draws my rect again.
EDIT:
here is a screenshot of the problem:
I sometimes face the same problem. I think the following is what I use.
/// .h
#interface BackgroundView1 : NSView {
NSImage *myImage;
}
// .m
- (void)awakeFromNib {
[self setupBackgroundImage];
}
- (void)setupBackgroundImage {
NSColor *c = [NSColor colorWithDeviceRed:0.0f/255.0f green:55.0f/255.0f blue:150.0f/255.0f alpha:1.0f];
if (myImage == nil)
myImage = [self createColorImage:NSMakeSize(1,1):c];
}
- (void)drawRect:(NSRect)rect {
[myImage drawInRect:NSMakeRect([self bounds].origin.x,[self bounds].origin.y,[self frame].size.width,[self frame].size.height)
fromRect:NSMakeRect(0,0,[myImage size].width, [myImage size].height)
operation:NSCompositeCopy
fraction:1.0];
}
// Start Functions //
- (NSImage *)createColorImage:(NSSize)size :(NSColor *)color {
NSImage *image = [[NSImage alloc] initWithSize:size];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:size.width
pixelsHigh:size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0];
[image addRepresentation:rep];
[image lockFocus]; // Lock focus of image, making it a destination for drawing
[color set];
NSRectFill(NSMakeRect(0,0,size.width,size.height));
[image unlockFocus];
return image;
}
// End Functions //
I want to draw image background in NSView and place some elements which also have transparent background on top of it.
I've overrided draw rect of NSView:
- (void)drawRect:(NSRect)rect
{
[NSGraphicsContext saveGraphicsState];
NSImage *image = [NSImage imageNamed:#"header_background.gif"];
[image drawInRect:CGRectMake(0, rect.size.height - 75, rect.size.width, 75) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
}
NSWindow init:
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag];
if (self) {
[self setOpaque:NO];
[self setHasShadow:YES];
[self setBackgroundColor:[NSColor clearColor]];
}
return self;
}
After that a've got such weird effect. Backgrounds of Sliders and buttons are also blinking sometimes
What am I doing wrong?
Well, first off, your “color” is ignored when drawing images, so that’s not doing anything. Second, the code I see here probably isn’t causing the problem, can you post the rest of the file?
Right now I’m suspecting maybe you have marked this view as being opaque, when it’s not, or that you have something else odd in your view hierarchy.
I have a UIScrollView with an image that needs to be scalable. I want to have a "footer" with a black (opaque) background and white text. I wanted to have it be fixed as a footer. It will be opaque so you can see the image behind it.
I created a containing UIView for the scrollview and footer. I can get the scrollview to be smaller than the app frame and have a footer at the bottom filling in the extra space, but obviously I can't see the image behind the footer.
I also tried putting the UIScrollView and UIView (footer) inside the container and positioning them accordingly, but in this case I can't even see the footer. Any ideas?
Code I've gotten so far (executed in viewDidLoad of view controller):
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
[view addSubview:scrollView];
[view addSubview:textView];
self.view = view;
I assume that with "image" you mean that there is an UIImage in the scroll view, and that you want to see it through a half transparent footer view.
In this case you should not put the views beneath each other, but the footer view on top of the scroll view. You can insure it is on top by calling bringSubviewToFront or using [containerView insertSubview:footer atIndex:0]; instead of addSubview:.
Another caveat might be that you call your view view. There might be some unpredictable behavior about which view it is, so perhaps it is better to call it something else and then assign it to self.view.
I have plenty of UIScrollViews with a small non-scroll view at the bottom or the top. In most cases I don't have any overlap, and they just sit side-by-side. I create them all in InterfaceBuilder, and assign them to IBOutlets.
I would like to create a center red tab button (like in Pinterest) as seen in the screenshot below
Any advise on how this can be done?
You can add a custom button on top of the UITabBar.
See http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/
//in ur view didload
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *viw = [[UIView alloc] initWithFrame:frame];
UIImage *image = [UIImage imageNamed:#"urRedColorImage.png"];
UIColor *color = [[UIColor alloc] initWithPatternImage:image];
viw.backgroundColor = color;
[color release];
[[self tabBar] insertSubview:viw atIndex:0];
[viw release];
}