I am using uibutton to smack that to interact it like fruit ninja ,i don't want to cut it , just interact the uibutton to smack it,how to implement that help me how to do that.
Instead of UIButton use a UIImageView and set it's userInteractionEnable property to true.
Add a swipe gesture to that imageView, It will solve your problem.
The control used is swipe gesture on the view ,Not the uibutton with button click event.
A nice tutorial
You can use the UISwipeGestureRecognizer for this.
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doSomething)];
[mSwipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:mSwipeUpRecognizer];
[mSwipeUpRecognizer release];
And in your .h file, add the delegate:
<UIGestureRecognizerDelegate>
Related
Hi i am new to iOS programming.
I need to create an photo library like iPhone native photo app. I have found a library MWPhotoBrowser
that provides nearly same UI for photo browsing and its perfect for my requirement. But now i have 2 problems
First is that i need to create a grid layout with thumbnails. Clicking on an thumbnail should display image in full screen with browsing functionality. I tried to dynamically adding UIImageViews in UIScrollView, but UIScrollView is not scrolling and images are going out of screen.
Second is i could not get any tap handler on UIImageView so that i can open an image in full screen.
Looking for some tips here, i am really stuck here.
You can give a shot to this library, it has both features which you are looking for.
https://github.com/gdavis/FGallery-iPhone
Hope it helps :)
For ScrollView scrolling issue you have to increase scrlViewMain.contentSize dynamically. Create a for loop and put bellow code at the end of loop.
scrlViewMain.contentSize = CGSizeMake(0, incermentAsPerYourNeed);
For the tapping issue you have to add TapGesture. Put bellow code when your ImageView creates.
imgView.tag = giveAsPerYourRecordsID;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[imgView addGestureRecognizer:tap];
[tap release];
And bellow your catch method for tapping.
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
UIImageView *tmpImgView = (UIImageView *)gestureRecognizer.view;
}
I have also developed similar functionality a few days ago for my project.
For tap issue you should use UIButton instead of UIImageView, it gives you tap action event. For Scrollview not scrolling, just make sure you update contentSize property of UIScrollView, otherwise it would not scroll according to your subviews.
I'm trying to realize an app that requires to move an UIImageView, that is put behind a transparent grid. How can I make it?
This is the code:
[[self view] addSubview:myimage];
myimage.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[panGesture setDelegate:self];
[myimage addGestureRecognizer:panGesture];
[self.view addSubview:over];
The transparent layer is "over".
If it is not added to the view, my pan method works.
When I add it, nothing works. So, how can I move my image behind the transparent grid?
Thank you everybody.
make over not clickable by setting userInteraction to no
if that doesn't work for some reason, use a custom class for over and override pointInside to always return NO :D
You can add aUIImageView as transparent layer over your image, and as UIImageView have by default userInteractionDisabled don't change that.
It's because if overlay image have user interaction then that would not let that pass to background view but 'll consume touches. Hence to enable panning on back ground view you need to pass touches on that view. By some approach. One of those is what I told you.
How can i use image to Perform an Action instead of Using Button or adding an image to Button, just wanna click button and perform particular set of instructions.
(Thanks in Advance)
Use a UIGestureRecogniser.
// IF your image view is called myImage
UIImageView *myImage = ...;
// Add a tap gesture recogniser
UITapGestureRecogniser *g = [[UITapGestureRecogniser alloc] initWithTarget:self action:#selector(imagePressed:)];
[myImage addGestureRecogniser:g];
[g release];
When your image is tapped, this method will get called
- (void)imagePressed:(UIGestureRecogniser *)recogniser {
NSLog(#"%#", recogniser);
}
Why don't you want to use a UIButton - it inherits from UIControl and has a lot of code that you probably don't even know exists? And it can just contain an image so it would look exactly the same?
Well, the pro about using UIButtons is that it got all touch events built right in. You can use UIImageViews, but you'll need to subclass them, while in most situations, a UIButton using a background-image would just fit.
I have a large UILabel which I am using to cover a bunch of buttons while I do something else.
All I have set initially is myLabel.hidden = YES; so you can't see the UILabel but the UIButtons (below it) won't work anymore.
Is there another setting for the UILabel I can use to allow touches to go "through it" when it is hidden? Thanks.
why use a UILabel to cover your buttons.
just set
UIButton *button;
[button setUserInteractionEnabled:NO];
or
[button setUserInteractionEnabled:YES];
You can use addSubView and removeFromSuperview methods :
When you want to hide your UIButton with your UILabel :
[self.view addSubview:myLabel];
and the contrary :
[myLabel removeFromSuperview];
I am not sure why a hide is stopping the touch events on buttons. Anyways you can explicitly bring the buttons to foreground by the following calls.
[self bringSubviewToFront:button];
The simplest thing to use as a general way to hide or cover things just a straight UIView. set:
[myCoverView setUserInteractionEnabled:YES];
and it will intercept touches and block touches to the buttons below it.
It should stop blocking touches when you hide it or turn the alpha to 0.0;
You can always siwtch the covering views interaction to:
[myCoverView setUserInteractionEnabled:NO];
and touches will pass through it.
If there is some reason that you need the UILabel these methods will work with it also.
[myLabel setUserInteractionEnabled:NO].
Even if is hidden, your label will get the touches anyway. You have to disable that to achieve what u want.
I think you should hide your buttons instead of covering them with a label.
[yourButton setHidden = YES];
[yourButton2 setHidden = YES];
...
Sorry for the messy title, I just don't know how to describe the problem in a delicate way.
I'm writing a album-like app to display a bunch of image in my scrollview and do something when a image is touched.
I followed this question : how can i detect the touch event of an UIImageView and use button with background image to handle touch event.
My original method is using NSOperation to concurrently fetch image from internet and put it io a imageview and add to my scrollview, and the speed is quite ok because each imageview shows right after each NSOperation callback.
Then I change imageview to uibutton, the strange thing is that when a NSOperation callback, that button does not show in my view. They show up at once when all the NSOperation callback is done. That makes the user experience become unacceptable.
This is my NSOperation callback function, it will pass a button that contains the image fetched from internet
- (void)imageLoaded:(UIButton*)button;
{
NSLog(#"Done");
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
The buttons will only displa after the last "Done" appear instead of one by one, is that normal? or I messed up something?
==== update ========
I think I'm running the NSOperation on my viewcontroller. I have a imageLoadOperation class, I'll pass my viewcontroller to it
imageLoadOperation *ilo = [[imageLoadOperation alloc] initWithURLString:[NSString stringWithFormat:#"link for the image"];
[ilo setParent:self];
[queue addOperation:ilo];
[ilo release];
And in the main function of imageLoadOperation I'll do
[parentViewController performSelectorOnMainThread:#selector(imageLoaded:) withObject:button waitUntilDone:NO];
Do you mean I need to move these code to my AppDelegate instead of running in my viewcontrollor?
You can use a button of custom type over your image view instead of using button with background image, or you can use touch event on an UIImageView
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
What if instead of converting to a button you just allowed your UIImageView to handle the touch event?
To add a touch event to a UIImageView use the following format in your .m
UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(newTapMethod)];
[_imageButtonName setUserInteractionEnabled:YES];
[_imageButtonName addGestureRecognizer:newTap];
then create the newTapMethod (or what ever you name it)
-(void)newTapMethod{
//...
}
hope that helps.