Change image when pressing uiimageview - uiimageview

i am doing a very simpel memory game to a ipad.
I want to change the image on the uiimageview when pressing it, and change it back when pressing it again.
Thanks for any help!
Regards

Using Tap gesture recognizer to change the imageview's image.I upload my code for tap gesture on image.
Note : Don't forget to imageview's UserInteraction is enable.
The below code write in your ViewDidLoad method.
UITapGestureRecognizer *firstdoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(firstTapGesture:)];
firstdoubleTap.numberOfTapsRequired = 1;
[_firstimageview setUserInteractionEnabled:YES];
[_firstimageview addGestureRecognizer:firstdoubleTap];
Than Your action method.
- (void)firstTapGesture:(UITapGestureRecognizer*)sender
{
_firstimageview.image=[UIImage imageNamed:#"abcd.png"];
}

Related

Adding a UILabel to a UIImageView

In my app, I am saving an image view with drawings on it. When I try to add the label as a subview to the image view, the image is successfully saved but does not have the label on it. Any thoughts? mainImage is the UIImageView and playNameDisplay is the UILabel.
Here is my code:
UIGraphicsBeginImageContextWithOptions(self.mainImage.bounds.size, NO, 0.0);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
[mainImage addSubview:playNameDisplay];
UIImage *SaveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(SaveImage, self,#selector(image:didFinishSavingWithError:contextInfo:), nil);
If you're using iOS7 you can use the new snapshotting api from apple.
UIGraphicsBeginImageContextWithOptions(image.size, NULL, 0);
[view drawViewHierarchyInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The view you call drawViewHierarchyInRect should be your UIImageView that has your UILabelView as a subview.If you have access to the Apple WWDC 2013 videos you should check out Implementing Engaging UI on iOS. It has some good code examples and also reviews the performance improvement over the more traditional methods.
Just try with bring subview to front property and check.Like this:
if Label is added as a sub view of image view use this.
[self.mainImage bringSubViewToFront:playNameDisplay];
if Label and image view is added as a sub view in view:(Nothing but both image view and label over laps) use this.
[self.view bringSubviewToFront:playNameDisplay];

how can i have imageView.animationImages combined with UISwipeGestureRecognizer

I have a viewController displaying images and text and the images are loaded and being animated every 15 seconds, I'd like to allow the user to swipe on the image view allowing him to change the image without waiting for the time to pass.
this is what I tried the images are loaded and animated but the swipe gesture doesn't work
self.imageView.animationImages = [self loadImages];
self.imageView.animationDuration = 15;
[self.imageView startAnimating];
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeAction:)];
[swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.imageView addGestureRecognizer:swipeRight];
this code is placed in the viewDidLoad method, I saw a solution adding the gesture to the view but I need it only on the imageView
I'm answering this in case someone else spends too much time on this one it appears i forgot to enable user interaction add this line:
self.imageView.userInteractionEnabled = YES;

How to change UIImageview image by clicking on that image instead of button click in Iphone?

How to change image by click on that image in Iphone.Instead of button click I want to click on image.Which method can I use UIImageView.
Use a button and make your picture the picture for the button. You can do all this in Storyboard and then right click the button and drag to the view you want to go to. It should work just like that.
After you drag the button to the storyboard go to the property inspector. Make the button a custom button in the drop down list and then use the image field to select your image. You will need to size the button correctly and set the fill property to "size to fit." the other button behaviors are all customizable if you don't want normal button behavior, i.e. visually reacting to a touch.
This can be done by using touchevents also
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([[touches anyObject] view] == self.m_ImageViewObjectName) {
//Here you can change the image of UIImageView
}
}
You have to set tapRecognizer for your ImageView. It'll detect tap on your ImageView and call the method -clickEventOnImage.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(clickEventOnImage:)];
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
//Don't forget to set the userInteractionEnabled to YES, by default It's NO.
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:tapRecognizer];
Add your clickEventOnImage: will look like this :
-(void) clickEventOnImage:(id) sender
{
[myImageView setImage:[UIImage imageNamed: #"myNewImageName.png"]];
}

Xcode change image on button that is not sender

I am trying to change a image of a button that is not the senders, I have tried to link the code to the buttons and it seems not to work
I tried to link the 3 buttons to the -(IBAction)Button1:(id)sender; it still does not work.
.h
-(IBAction)Button1:(id)sender;
-(IBAction)ButtonUn1:(id)sender;
-(IBAction)ButtonDef1:(id)sender;
.m
-(IBAction)Button1:(id)sender
{
UIImage *btnImage1 = [UIImage imageNamed:#"button_Not_At_all_R.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
UIImage *btnImage2 = [UIImage imageNamed:#"Button_Unsure.png"];
UIImage *btnImage3 = [UIImage imageNamed:#"Button_Definitely.png"];
[ButtonUn1 setImage:btnImage2 forState:UIControlStateNormal];
[ButoonDef1 setImage:btnImage3 forState:UIControlStateNormal];
}
Thanks for any help.
You need to create an IBOutlet for your button and link it to access it directly:
IBOutlet UIButton* buttonToChangePicture;
The IBAction ist only used to access an event.
Edit:
Another option is to use viewWithTag.
UIButton* button = (UIButton*)[myView viewWithTag:555]
You need to set a unique tag for your button first (555 in my example)
There is no other way to access another button in an IBAction

dismiss UIPopoverController (if visible) after tapping a UIBarButtonItem

I'm trying to dismiss any currently visible UIPopoverControllers if/when another UIBarButtonItem is tapped as seen on Pages for the iPad when tapping between Styles/Media/Tools etc.
I've done this, but it's too specific. Looking for something generic.
if ([popoverController isPopoverVisisble]) {
[popoverController dismissPopoverAnimated:YES];
}
Thanks!
Did you set the passthroughViews property of the popover controller? If you do this, then taps outside the popover will not cause the popover to automatically dismiss, but will instead be sent to the views in that array. You should be able to add the UIBarButtonItem to this array, and then dismiss the popover in that handler.
randallmeadows answer will NOT work. UIBarButtonItem is not descendant of UIView, which means you cannot add it to passthroughViews.
The only solution I found for now is to create UIBarButtonItem with custom UIButton using
UIBarButtonItem *b = [[UIBarButtonItem alloc] initWithCustomView:button]
and then
popoverController.passthroughViews = [NSArray arrayWithObject:b.customView];
But be prepared that you'll loose all the styling - you cannot create UIButton that looks like UIBarButtoItem too easily.
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.passthroughViews = #[];
Works for me

Resources