change and show background image in ios - objective-c

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

Related

Use color with alpha fill NSView?

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.

How to make image view as button in Objective-C?

For example:
Collection view have four images,The user touches any cell mean its want to go to another view controller.give some idea for me friends.
Take this image as example if the user click the chrome image means it go to another view controller.
Create new UIButton, then use setImage method on it to set the image. Leave the title empty.
use this code for objc:
-(void)viewDidLoad {
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
[imageview setImage:[UIImage imageNamed:#"image.png"]];
[imageview setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTap setNumberOfTapsRequired:1];
[imageview addGestureRecognizer:singleTap];
[self.view addSubview:imageview];
}

UIButton does not fire target on touchUpInside unless I drag just outside?

I have a Button that when I press it, it does not fire the target selector that I've added. I've made the button Different Images, so I can see that I am Pressing it.
So here is the Funny if I Press, and drag outside of the button, but not outside of the button's immediate superView, the target selector is fired!
I've also in testing set the setClipsToBounds:YES on all the super/sub views just to be sure it was still in the views bounds. Seems to be within bounds. The dragging outside the button area seems to be omni directional, so its not like I can only tap/drag right. Left up and down work too. I can tap, drag out then back in and it works. If I don't start to drag and just tap and hold, the button highlights and then goes back to unselected state.
Here is the code for the Button. The Button is on a UIView along with a UITextView, Actually onTop Of the UITextView. The UIView that all of this is on is on a Larger View, which is on a Scaling/Scrolling view
messageLookupButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
[messageLookupButton setTitle:#"junk" forState:UIControlStateNormal];
[messageLookupButton sizeToFit];
[messageLookupButton setTag:kGearButtonView];
//Get the images for the buttons
UIImage *gearButtonImage = [UIImage imageNamed:#"gear.png"];
UIImage *gearButtonImage2 = [UIImage imageNamed:#"StatusLightOn.png"];
[messageLookupButton setImage:gearButtonImage forState:UIControlStateNormal];
[messageLookupButton setImage:gearButtonImage2 forState:UIControlStateHighlighted];
[messageLookupButton addTarget:self action:#selector(gearPressed:) forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(processLookupResults:)
name:kRefreshFromPopover object:nil];
[self addSubview: messageLookupButton ];
[messageLookupButton release];
[messageLookupButton setCenter:CGPointMake(CGRectGetMaxX(elementViewContents.bounds) -kElementContentFrameOffset -(CGRectGetWidth( messageLookupButton.bounds)/2), CGRectGetMidY(elementViewContents.bounds))];
[self bringSubviewToFront:messageLookupButton];
The Scroll View has several gesture recognizers on it. Though they don't seem to interfere with other buttons and controls that I've put on the screen. Though I have a feeling its the Scroll View that is the problem.
scroll view code clip:
[scrollContentView setUserInteractionEnabled:YES];
[scrollContentView setExclusiveTouch:YES];
[scrollContentView setCanCancelContentTouches:YES];
[scrollContentView setDelaysContentTouches:YES];
I ended up Just adding the gear as an Image to the View, then created a invisible button that was the same size as the UIView that I placed the gear icon on.
-(void) addMessageLookupSelector {
// Set a default Color if there is none
if (_isMessageLookupField ){
// Add Gear to End Of Text Field
if (!messageLookupImageView) {
messageLookupImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
[messageLookupImageView setTag:kGearButtonView];
[messageLookupImageView setImage:[UIImage imageNamed:#"gear.png"] ];
[self addSubview: messageLookupImageView ];
[messageLookupImageView release];
}
[self positionMessageLookupImageView ];
}
[self setNeedsDisplay];
}
- (void)makeControlEditable {
//Setup the TextField so we can edit its value, need to see if there is a Write Animation Assocated with Element
[self setTheWriteTag];
if (writeTag) {
writeInputRequestedButton = [UIButton buttonWithType:UIButtonTypeCustom];
[writeInputRequestedButton setFrame:elementViewContents.frame];
[writeInputRequestedButton setUserInteractionEnabled:YES];
[writeInputRequestedButton setEnabled:YES];
// [writeInputRequestedButton setBackgroundColor:[UIColor clearColor]];
[self addSubview: writeInputRequestedButton];
[self bringSubviewToFront:writeInputRequestedButton];
[writeInputRequestedButton addTarget:self action:#selector(wantsToEditValue:) forControlEvents:UIControlEventTouchUpInside];
}
}

How to get UIView to overlay UIScrollView as a footer

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.

Button not clickable after adding as a subview to a UIView

I have created a class called UICustomButton, which is a subclass of UIView. I added a UIButton to UIView as a subview as shown in my code below:
-(id)initWithButtonType:(NSString *)type
{
self = [super init];
if (self)
{
self.customButton = [self setupButtonWithTitle:type andFrame:frame];
[self addSubview:self.customButton];
self.customButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
The problem I am facing is that although the button appears, they are not clickable. Is there something wrong with the way I am adding the buttons to the UIView?
EDIT: To add on, I am using this custom button class instance to a cell:
UICustomButton *customButton = [[UICustomButton alloc]initWithFrame:someFrame];
[cell.contentView addSubView:customButton];
check the frame of your UICustomButton object, and if self.customButton is out of its superView.
Make sure that the frame of the subview is within the frame of its superview. I encountered the issue twice and both times the frame of the subview was incorrect.
You should check if all properties userInteractionEnabled are on:
Check that property for UITableView where your cells with this view are displayed
Check that property for UITableViewCell where you add this view as subview
Check that property for UICustomButton
Check that property for customButton in -(id)initWithButtonType:(NSString *)type
My button was not working because i had two views in each other. The first view, as you can see, has a width and heigth of 0 pixels. I've made this view the same size as view2 and then my button was clickable.
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(325, 346, 0, 0)];
view1.alpha = 0.90;
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 450, 150)];
[view2.layer setCornerRadius:10.0f];
view2.backgroundColor = [UIColor whiteColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:#selector(addClosedToCollection) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(30, 30, 300, 32)];
[btn setTitle:#"i'm a button" forState:UIControlStateNormal];
[view2 addSubview:btn];
[view1 addSubview:view2];
i hope i've helped somebody out :)
Your outer view probably has a height of 0. Add constraints that makes it the same height as (or higher than) the subviews, or create it with a frame that is large enough.
try putting this after the if statement:
self.isTouchEnabled = YES;