My custom button touchupinside event not fire when I put some code in my custom button
Note: When I tap on button this method is fire but touchUpInside method not fire
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(#"touchBegan");
[Singleton playSoundForEvent:Sound_ButtonTap];
}
If I remove above code then working fine.
Above code reason : I don't put tap sound everywhere on touchupinside.
I would like only One line code in whole project.
You forgot the call to super
[super touchesBegan:touches withEvent:event];
The reason it doesn't work without the call to super, is that a button needs to register touch events (touchesBegan, touchesEnded, touchesMoved, touchesCanceled) to be able to map them to UIControlEvent, and only then fire actions with sendAction.
Full code
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(#"touchBegan");
[Singleton playSoundForEvent:Sound_ButtonTap];
}
Please check this code and test again I hope It will working fine.
[super touchesBegan:touches withEvent:event];
Related
I have a segmented control,with 5 segments..I want some event to be fired when a touch down happens on any of the segment and i mean tap and hold,without lifting the finger.
And when the user lifts his finger,it should reset
I have tried using touchesBegan and touchesEnded but i don't get the current selectedIndex in touchesBegan,here's my code
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
long oldValue = self.selectedSegmentIndex;
[super touchesBegan:touches withEvent:event];
if (oldValue == self.selectedSegmentIndex )
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
NSLog(#"selectedIndex%lu",self.selectedSegmentIndex);
[super touchesEnded:touches withEvent:event];
self.selectedSegmentIndex = -1;
}
Is there any way to achieve a touch down event effect for a segmentedControl,or any other alternative?
You can use touchDown: IBAction selector.
You can try to override:
- (void) setSelectedSegmentIndex:(NSInteger)toValue
Quite frankly, I do not think it is possible to do with UISegmentedControl. You do not get selected segment index until touchesEnded:withEvent: is called.
If you want to get in touchesBegan:withEvent:, my advise would be to write your own custom view that look like UISegmentedControl and gives you call back on touchesBegan:withEvent:.
As suggested by kientux above, adding transparent views or buttons on top of each segment in UISegmentedControl could be another potential solution to your problem but it would be very very tricky!
Good luck!
I have some custom animations in my UIButton subclass. I override some inner methods for starting and ending animation
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
//there is my start animation
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
//there is my end animation
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
//there is my end animation
}
There is almost all good. All my buttons make some actions on event UIControlEventTouchUpInside. And there is my problem. Methods touchesEnded and touchesCancelled are called only after my app handle UIControlEventTouchUpInside event. So animations in button looks not so nice like I want.
So question is: can I delay UIControlEventTouchUpInside event on some ms and firstly handle touchesEnded method of my button for smooth animation?
I am trying to create a custom swipe gesture recognizer to set allowed angles and distances.
So far it works really well but needs a lot of code in the view controller. So my question is wether it is possible to somehow pass a method call like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
in the view controller to my gesture recogniser object so that I don't have to write something like this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.swipeGestureRecognizer touchesBegan:touches withEvent:event];
}
into my view controller. I think the build in UIGestureRecognizer does this somehow but I could not figure out how it works.
I really would appreciate your help.
[EDIT]
Generic example: I have an object A which creates an object B. A has a method c that gets called sometimes. What I want to achieve is that a method d of B gets called when c in A gets called. So to somehow forward or pass this method call.
Specific example: The following method gets called in my view controller every time there is a touch on the screen:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
but I actually want my GestureRecognizer object to process this information so I have the same method in my GestureRecognizer object and I want this method to get called every time the counterpart in the view controller gets called.
Sub class UIGestureRecognizer and create a class called CustomGestureRecognizer.
Its .m file will look like this
#implementation CustomGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// do your code here
}
#end
Some where in the subclass you have to change the state of the recognizer to
UIGestureRecognizerStateRecognized
when the current gesture is registered, else change to
UIGestureRecognizerStateFailed
.
Now in the view controller where you want to use this recognizer do :
CustomGestureRecognizer * recognizer = [[CustomGestureRecognizer alloc] initWithTarget:self action:#selector(handleChange:)];
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];
Detailed explanation is given here Apple doc, under subclassing.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.swipeGestureRecognizer touchesBegan:touches withEvent:event];
}
The above will end up with Recursive calls, and your program will crash at some point of time.
Is there a way I can query a UIView to determine if it currently being touched? The reason I ask is because I am using "touchedBegan", "touchesEnded", and "touchesMoved" to keep a UIView under a user's finger. However, if the user moves his/her finger really fast and manages to "escape" the window I want to remove that window.
I was thinking I could use a timer to periodically test each view to determine if it is currently being touched, if it is not, I will remove it. An "IsTouchedProperty" would be perfect.
Any thoughts?
I had a similar problem and solved it by using the tracking property. From the documentation:
A Boolean value that indicates whether the receiver is currently
tracking touches related to an event. (read-only)
This is a method on UIControl, but aren't you trying to create one?
You can also hitTest views. Check the apple docs
Since UIView doesn't inherit from UIControl you would need to subclass UIView and roll your own isTouching property using touch events. Something like:
// MyView.h
#interface MyView : UIView
#property (nonatomic, assign) BOOL isTouching;
#end
// MyView.m
...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
self.isTouching = YES;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
self.isTouching = NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
self.isTouching = NO;
}
I am dtecting touches on my UIView.
Is some situations I want to be abel to cancel touches so that touchesEnded won't get called. But doesn't matter what touchesEnded will always get called?
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (SOMETHING_SPECIAL)
{
[super touchesCancelled:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//I don't want it to get here if touches were canceled how can i do this?
}
- In my touchesEnded how can I determine whether touches were canceled or not?
TouchesEnded will always get called wherever your touches where canceled or not, so I would suggest instead having that exact:
if (SOMETHING_SPECIAL)
{
}
In your:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
To get the touchesCancelled event, implement this method from UIResponder.
touchesCancelled:withEvent:.