How to find out how many times a user touched an image - uibutton

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchlocation = [touch locationInView:self.view];
How would you know when a user touched a certain button and how many times if i have four different buttons.

I'm into C# so forgive me if I'm wrong, but couldn't you just increase an integer whenever the user clicks on an image, then if you wanted to know the amount of clicks, you'd look at this integer?

what you will have to do is take one integer in.h file then in .m file write touchesbegan method like below.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchlocation = [touch locationInView:self.view];
if([touch isKindOfClass:[UIImage class]])
{
myInt++;//your global integer....
}
}
let me know it is working or not!!!
Happy Coding!!!!

Try to use this one..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
// ----------you can find using tag and first set button's tag in xib ------
if(touch.view.tag == btnRed.tag)
{
redTouch++:
}
}

Related

Changing SKSpriteNode property via touchesBegan

I was wondering if there was a more efficient way to changing the property of a SKSpriteNode in touchesBegan than my current method. The following is my method:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"Start"]){
for (SKSpriteNode *node2 in [self children]){
if ([node2.name isEqualToString:#"Start"]){
node2.texture = [SKTexture textureWithImageNamed:#"button_beige_pressed"];
}
}
}
...
}
Your current logic suggest that you have multiple nodes with the name 'start'. If this is actually the case I suggest creating an array and storing all 'start' nodes in said array.
If however you only have a single node with the name 'start', then no loop is needed as you already have a reference to the node with the touch function.
I found a solution:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKSpriteNode *node = (SKSpriteNode *)[self nodeAtPoint:location];
if ([node.name isEqualToString:#"Start"]){
node.texture = startPressed;
}
}
I was using a Node when I should have been using a spritenode.

How to drag and move UITextFields?

I have it set up where i can move multiple labels. I'm having problems moving a UITextfield. I made sure that user interaction and multiple touch is enabled.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([touch view] == tex1) {
tex1.center = location;
Try to make this trick, it works for me
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *clearView = [[UIView alloc] initWithFrame:tex1.frame];
[clearView setTag:101];
[self.view addSubview:clearView];
[clearView release];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] tag] == 101 && [touch tapCount] == 2) {
[tex1 becomeFirstResponder];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([[touch view] tag] == 101) {
tex1.center = location;
[[touch view] setCenter:location];
}
}

Do i need to release UITouch objects?

I have a method such as this:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//Use to animate drag
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
[self colorDragedOver:currentLocation];
[touch release];
}
Do I need to release either the UITouch or the CGPoint to prevent any leaks?
Thanks!
No. You never alloc/init or copy the touch so it is considered autoreleased.
CGPoint isn't an object or a reference so should never be released or autoreleased.

how to let the touchesMoved only control one view?

i create an UIButton on the view,and i want to let the touchesMoved only control the UIButton,not the whole view
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self.view];
}
i want to do like this
if i touch the UIButton,and then the UIButton can be moved with my finger,if i touch other view and my finger is moving on the screen,the UIButton does nothing.
that means the function touchesMoved only has the role of the UIButton,so how can i do it? thanks
I presume the code you show takes place in your custom view controller subclass, and the UIButton is a subview of its view.
Define a simple BOOL in your class that you set to NO first. Then update it in the event handling methods.
// .h
BOOL buttonTouched;
// .m
// in the viewDidLoad
buttonTouched = NO;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// test wether the button is touched
UITouch *touch = [touches anyObject];
CGPoint touchBegan = [touch locationInView:self.view];
if(CGRectContainsPoint(theButton.frame, touchBegan) {
buttonTouched = YES;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(buttonTouched) {
// do it here
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self.view];
CGRect newFrame = CGRectMake(touchMoved.x,
touchMoved.y,
theButton.frame.width,
theButton.frame.height);
theButton.frame = newFrame;
}
}
// when the event ends, put the BOOL back to NO
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
buttonTouched = NO;
}

UIScrollview getting touch events

How can I detect touch points in my UIScrollView? The touches delegate methods are not working.
Set up a tap gesture recognizer:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
and you will get the touches in:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}
You can make your own UIScrollview subclass and then you can implement the following:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"DEBUG: Touches began" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"DEBUG: Touches cancelled");
// Will be called if something happens - like the phone rings
UITouch *touch = [[event allTouches] anyObject];
[super touchesCancelled:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"DEBUG: Touches moved" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"DEBUG: Touches ending" );
//Get all the touches.
NSSet *allTouches = [event allTouches];
//Number of touches on the screen
switch ([allTouches count])
{
case 1:
{
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch([touch tapCount])
{
case 1://Single tap
break;
case 2://Double tap.
break;
}
}
break;
}
[super touchesEnded:touches withEvent:event];
}
If we're talking about the points inside the scrollview then you can hook with the delegate method:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
and inside the method, read the property:
#property(nonatomic) CGPoint contentOffset
from the scrollView to get the coordination.
This works also on touch down event.
In the currently as correct marked answer you can get a touch point only at a "Tap" event. This event seems only to fire on "finger up" not on down.
From the comment of yuf in the same answer you can get the touch point from the underlying view also within the UIScrollView.
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
CGPoint touchPoint = [touch locationInView:self.view];
return TRUE; // since we're only interested in the touchPoint
}
According to Apple's documentation the gestureRecognizer does:
Ask the delegate if a gesture recognizer should receive an object representing a touch.
which means to me that I can decide if a gestureRecognizer should recieve a touch or not.