How to perform some Action on imageview clicked? - iphone

I'm working on a photo Collage application. I want to perform an action on image view so any one can help me to solve my problem. when i pick image from photo library and shown on image view after when double click on image view i want to push or present a new view to crop this image .....so anyone tell me what i do to solve my problem...

Use a UIButton of type custom and put an image inside it. And you are done.

Add the Gesture for ImageView
UITapGestureRecognizer *sg=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(HandleEvent)];
sg.numberOfTapsRequired=2;
[BottomBar addGestureRecognizer:sg];
[sg release];
and do your task here:
-(void)HandleEvent
{
// your task
}
Thanks

You should use a UIButton instead of an UIImageView. You can set its style to custom and then asign it an image. UIButton provides all the methods for double tap already built in.

If you are using UIImageView, make sure you use setUserInteractionEnabled:YES. Then you need to check your touches in touchesBegan, ended, (canceled).

You are set on using UIImageView, you will have to subclass UIImageView and override the touch methods you want to intercept. For example:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Call super to make sure you don't lose any functionality
[super touchesBegan:touches withEvent:event];
// Perform the actions you want to perform with the tap.
}
Using UIButtons as replacements for your UIImageViews is a great idea you can just drag a connection from each button to an outlet in your view controller and handle the action. You can set the background image on the buttons as you are doing currently with the UIImageViews. Set your button type to custom and you'll be good to g

If you are using UIImageView class ,first set setUserInteractionEnabled:YES . Then set UITapGestureRecognizer event to image view.

try this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
CGPoint viewPoint = [imageView convertPoint:locationPoint fromView:self.view];
if ([imageView pointInside:viewPoint withEvent:event]) {
//do something
}
}

Make a button, and clear its color. On the back of button, put your image view, so that your imageview seems to be visible and the button doesn't. Now attach event to the button.

Related

Disable touch events on certain areas of iPhone screen

I want to disable touches on all areas of the screen apart from a specific few points (e.g buttons). I.e. I don't want 'touchesBegan' to trigger at all when I tap anything other than a button. Calling
self.view.userInteractionEnabled = NO;
has the desired effect for not registering touches, but then of course I can't tap any buttons. I basically want the button to still work, even if there are 5 points touching the screen, i.e. all touch inputs have been used up, and the button represents the 6th.
Is this possible?
I've tried inserting a view with userInteraction disabled below my buttons, but it still registers touches when the user taps the screen. It seems the only way to disable touch registering is to do so on the entire screen (on the parent UIView).
UPDATE:
I've tried using gesture recognizers to handle all touch events, and ignore those that don't qualify. Here is my code:
#interface ViewController : UIViewController <UIGestureRecognizerDelegate>
...
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *allRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:nil];
allRecognizer.delegate = self;
[self.view addGestureRecognizer:allRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint coords = [touch locationInView:self.view];
NSLog(#"Coords: %g, %g", coords.x, coords.y);
if (coords.y < 200) {
[self ignoreTouch:touch forEvent:nil];
return TRUE;
}
return FALSE;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"%i touch(es)", [touches count]);
}
However the screen still 'reads' the touches, so if I place 5 fingers down, the 6th one won't trigger a button press...
You need to set up an invisible UIButton and lay it between the view that should not register touches and the UIButtons that should still be active.
Now you need to set the invisible button's 'userInteractionEnabled':
//userInteractionEnabled == NO => self.view registeres touches
//userInteractionEnabled == YES => self.view doesn't register touches
[_invisibleButton setUserInteractionEnabled:NO];
What really matters in this solution is that both - the invisible and the visible buttons are direct subviews of the VC's view.
You can download an example project from my dropbox:
https://dl.dropboxusercontent.com/u/99449487/DontTapThat.zip
However this example just prevents the handling of certain touches. Completly ignoring input isn't technically possible: Third party apps are not responsible for for detecting input. They are just responsible for handling input. The detection of touch input is done iOS.
The only way to build up a case like you describe it in the comments is to hope that iOS won't interpret the input of your case as a "finger" because it's most likely going to cover an area that's way bigger than a finger.
So in conclusion the best way would be to change the material of the case you're about to build or at least give it a non conductive coating. From a third party developers point of view there is no way to achieve your goals with software if there is a need for 5 fingers as described in the comments.
There is couple of methods in UIView that you can override:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; // default returns YES if point is in bounds
This should prevent to call touchBegin and other methods.
Regards.
I have a nice solution for this. What ever the area you want to hide the interaction place a transparent button on top of the area.
touchesBegan is a default method so it must call all the time when touch happens on view so there is no way-out, But you can still do one thing set
self.buttonPlayMusic.userInteractionEnabled = FALSE;
for the object you don't need touch may be this could be help you with your desired output.
Have you tried using a UIScrollView as the background ? i.e the area where you do not want touch events to be fired.
UIScrollView does not call the touch methods.
You can add UIImageView Control on that area where you want to disable touch event.you can add UIImageView object as top of self.view subViews.
Example
//area -- is that area where you want to disable touch on self.view
UIImageView *imageView=[[UIImageView alloc]initWithFrame:area];
[self.view addSubView:imageView];
You touchDelegate will always call in this way, but if you are doing some task on touch then you can do your task like this way.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIButton *touchObject=(UIButton*)[touch view];
if ([touchObject isKindOfClass:[UIButton class]])
{
//Do what ever you want on button touch
}
else{
return;
}
}

Tapping on Buttons over MapView is zooming the Map..?

I have a MapView, Over which i have added a UIView (0, 216, 320, 200) contain 10 UIButtons. This UIView is to work like a menu, and Shows and Hides on a certain button action.
The problem here is, While the Menu is showing, when i tap a button single ime, it works perfectly, but tapping it very quickly multiple times makes the MapView Zoom(Which is under the UIView which contain the button). It means the touch event is passing to MapView.
Detail: Tapping on the button very quickly cause in MapView Zoom, and the Button Action performs when you stop tapping.
How can i prevent that?
What you could do is disable user interaction for the duration of your button's handler. So in the selector do:
-(void)buttonSelector:(id)sender {
_mapView.userInteractionEnabled = NO;
// ... do what your button needs to do
_mapView.view.userInteractionEnabled = YES;
}
Of course, this assumes your button gets the event before the map view... which I think it should because it's on top of the map view.
To handle the rapid tapping, you could create a subclass of UIButton and use the following code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_mapView.userInteractionEnabled = NO;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_mapView.userInteractionEnabled = YES;
}
In the builder change the class of the UIButton to your subclass. If you debug the touch in the set passed to the touchesEnd method and look at the tapCount, you'll see all your taps have been caught here.
Yes, so the problem was, I was adding UIView as subview over MapView.. I fixed it by adding the UIView as subview on self.view

How to handle touch event on UILabel as subview of UITableViewCell?

My app has a custom UITableView. In the cellForRowAtIndexPath delegate method of its UIViewController I am instantiating custom UITableViewCell objects that contain multiple custom UILabels (actually a sub-class of OHAttributedLabel) as subviews of the content view.
I have tried setting userInteractionEnabled = YES on the label, then adding touch events in the view controller, but that isn't working.
Ideas?
Thanks
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([self.site frame], [touch locationInView:self.view])){
//do whatever you want
}
}
Or
UILabel *label = =[UILabel alloc]init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTap)] autorelease];
[label addGestureRecognizer:tapGesture];
A UILabel isn't a UIControl so you won't get events on UIControlEventTouchUpInside or similar. Why not use a button instead? You can make it look exactly like a label.
Regardless you will probably need to set addTarget:action:forControlEvents: and tag on the UIButton in your cellForRowAtIndexPath: method. In that method, detect which cell's button was tapped by examining the tag value.
If you must use UILabel then you need to subclass it and intercept the touchesBegan/touchesEnded methods (inherited from UIResponder) to detect UIControlEventTouchUpInside yourself.
Problem in OHAttributedLabel. This label also handles tap on links. So for handle tap on any point of label (not just link) you must
self.textLabel.onlyCatchTouchesOnLinks = NO;
Where self.textLabel is your OHAttributedLabel.
And don't forget of userInteractionEnabled.
I don't know if it is the same problem but... I added a label and could not get it to recognize a touch, I eventually realised that it was because I was adding it as a subview, but its frame was outside its parent's frame, hence the touch heirarchy broke
I just had the problem with using static table cells for a settings table where I wanted the whole cell to trigger the first responder for the cell's textfield.
I ended up adding a transparent (custom, blank title) button behind the label (touch disabled) and textfield after not getting any touches using gesture recognizers. I think it should work in a more elegant way, but it solved the task for now and that limited purpose. (and you can just drag connect from the button's default action)
Bit ugly. Then again, it just describes the area behind the text field reacting to touch. Which was the intention after all. So maybe its just not that fancy.
Will keep it until I find the reason for the recognizers not firing.
you can use TTTAttributedLabel to instead it. it's very easy.
when you initial the UITableViewCell,you can delegate:TTTAttributedLabelDelegate
like :
#interface MyTableViewCell : UITableViewCell<TTTAttributedLabelDelegate>{
UILabel *nameLabel;
TTTAttributedLabel *valueLabel;
}
when you initial ,you could add link to label :
[valueLabel addLinkToPhoneNumber:valueStr withRange:NSMakeRange(0, valueStr.length)];
so,you could do anything you want:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber{
//do anything you want.
}

How to animate the objects in iphone sdk?

I'm using UIImageViews. I'd like this behaviour: if we touch the UIImageView it needs to zoom double and if the user remove his finger set the UIImageView to the actual position of the finger... How can I do?
Can any one please tell me the code for this?
Thanks in advance
U can use UIScrollView for this, with UIImageView as a sub-view of the scroll view.
For this, U need to sub-class UIScrollView to write a custom scroll view.
It will have all the properties of UIScrollView. Only thing, U have to override touchesEnded: method, which will notify the superview. So in the superview, again u can set the zoomscale to 1, to make it to actual position.
implement the delegate methods touchesBegan:withEvent: and – touchesEnded:withEvent:
in touches began method change the frame of the imageview to double the size of the imageview and in touchesEnded change the frame to the original size.
while declaring the image enable user interaction on it
UIImageView *imageview=[[UIImageView alloc] init];
imageview.userInteractionEnabled=YES;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
imageview.frame=CGRectMake(0,0,20,20);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
imageview.frame=CGRectMake(0,0,40,40);
}
TNX

Ignore touch event, and let the view below it handle the touch?

I have a draggable view that a user will touch, but some rectangles of it will have no image (alpha 0).
When a user clicks the transparent region (I am able to construct the transparent region without the alpha info), I want the view (same class) below the transparent region to detect the touch.
My strategy is to let the view ignore the touch when user touches the transparent area and hope the view below it will automatically catch the touch event. But I'm not sure if this will work. (setting things up to test this will take some time)
Should I take a different approach or the above strategy would work?
Thank you.
Try overwriting the method hitTest:withEvent: in the superview. You can make hitTest:withEvent: return the view you want to handle a given event.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *v in self.subviews){
CGPoint pointInB = [v convertPoint:point fromView:self];
if ([v someConditionYouMayWantToTestFor]){
return v;
}
}
return nil;
}
The method someConditionYouMayWantToTestFor is where you test if you want the subview to capture the event or not.