I would like to know how can I send a swipe gesture programmatically without actually swiping the phone. For instance, I have button that in the onClick event I would call swipeGestureRecognizer? Is this possible?
You can call the method you are calling on Swipe, when user taps on button. For examaple, you have a method for swipe gesture, call it onSwipe . Now call onSwipe methos when user taps on the button. Thats it
EDIT
Here is the code for you:
-(void)myMethod{
//Write the logic you want to implement for swipe gesture here.
}
-(IBAction)onClick(UIButton *)sender{
[self myMethod];
}
-(IBAction)onSwipe:(UISwipeGestureRecognizer *)recognizer{
[self myMethod];
}
There might be bugs on the code as I am just typing the code using windows. Correct it for yourself in MAC & edit the answer too. it would definitely work for you
If you need to pass the gesture event to the handler, then
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] init];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self handleSwipe:gesture];
Related
I am new at iPhone programming (Objective C). I wanted to make button which will be visible on long pressing on another button. Just like system keyboard style. I should choose first or second button by holding my finger on them :-) How can I do this, I could not find tutorials. Thanks
create and attach the UILongPressGestureRecognizer instance to your UIButton.
-(IBAction)SelectImage:(id)sender
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressMethod:)];
[self.button addGestureRecognizer:longPress];
}
Then create the method that handles the gesture
- (void)longPressMethod:(UILongPressGestureRecognizer*)gesture
{
if ( gesture.state == UIGestureRecognizerStateEnded )
{
NSLog(#"Long Press");
//Do your code here what you want to perform
}
}
I have a UIButton which is acting as a "confirm" button. Basically, I want it to require two taps to trigger its action, so the first time you tap it, the picture will change to an "are you sure" type image, at which point, a second tap would trigger the action.
This works fine, but I want to set it up so that tapping anywhere outside of the button resets it back to the first image again so it requires 2 taps again.
Is there any way to detect if the user touches outside of a UIButton; perhaps is there a way to give it "focus" and check for a focus exit?
I thought of UITouch, but that will only send its event to the view you are touching that responds to it.
Attach a UITapGestureRecognizer to the button's superview (probably the view controller's content view.) Set userInteractionEnabled to true. Put your code that resets the button in the handler for the tap gesture recognizer.
As you know you can use UIControlEvent.touchUpInside for when the view is pressed
If you want to see when a press is outside of a view you can you UIControlEvent.touchUpOutside instead
button.addTarget(self, action:<SELECTOR>,forControlEvents: UIControlEvents.TouchUpOutside)
You can add UITapGestureRecognizer to get user touch outside button event
#interface YourViewController ()
#property NSInteger tapCount;
#property (weak) UITapGestureRecognizer *gestureRecognizer;
#end
add UITapGestureRecognizer
- (IBAction)buttonPressed:(id)sender {
self.tapCount ++;
if (self.tapCount == 1) {
// change button label, image here
// add gesture gesture recognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOutSideAction)];
[tap setNumberOfTapsRequired:1];
[[[UIApplication sharedApplication] keyWindow] addGestureRecognizer:tap];
self.gestureRecognizer = tap;
} else if (self.tapCount == 2) {
// tap two times
self.tapCount = 0;
}
}
- (void)tapOutSideAction {
// reset button label, image here
// remove gesture recognizer
self.tapCount = 0;
[[[UIApplication sharedApplication] keyWindow] removeGestureRecognizer:self.gestureRecognizer];
}
I want to make a tableview in which i want jobs will change to swipe left and right .
By default in iphone swipe is for delete,so how can i make this.
i used gesture for that, but it does not look any transition in tableview,just swipe only,I want swipe transition will show like we are swapping.
Please help !
Method For Left Swipe
UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[yourtableviewName addGestureRecognizer:swipeleft];
Method For Right Swipe
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(swiperight:)];
[gesture setDirection:UISwipeGestureRecognizerDirectionRight];
[tblvie addGestureRecognizer:gesture];
left Swipe Action Method
-(void)swipeleft:(UISwipeGestureRecognizer*)recognizer
{
int tag = (int)recognizer.view.tag; // assign the tag if u need
if (tag==40)
{
// call the another function
}
else
{
// call the some another function
}
[yourtableview reloadData];
}
Right Swipe Action Method
-(void)swiperight:(UISwipeGestureRecognizer*)recognizer
{
int tag = (int)recognizer.view.tag; // assign the tag if u need
if (tag==40)
{
// call the another function
}
else
{
// call the some another function
}
[yourtableview reloadData];
}
I need to add several swipe gestures to my scene on a game. From what I know, this can only be done programmatically, unfortunately I have never added gestures in this way, I have always used IB. I know I need to initialize a gesture recognizer, using initWithTarget:action: and I know how to set its properties, what I don't know is how to make this gesture do stuff. I assume it is through the action parameter #selector but this seems to never get called. Am I doing this wrong? here is what I have:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(animateSwipeRightLeft)];
[self.view addGestureRecognizer:swipeRight];
and for the selector I have:
-(void)animateSwipeRightLeft {
//do stuff...
}
so it comes down to a few related questions: am I setting this up correctly? if so, why is my selector not being called? and if I am wrong about not being able to do this with IB, how?
also, if it helps, my gestures are set up in the initWithSize method for my scene.
You should add gesture recognizers in SKScene's didMoveToView method.
- (void)didMoveToView:(SKView *)view {
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(animateSwipeRightLeft)];
[self.view addGestureRecognizer:swipeRight];
}
self.view is nil inside init methods
I have put a UIView in my navigation item and I am using the JSBadgeView on it. I want for a different screen to appear upon clicking it, but I'm not sure how. iOS won't let me make a seque and I checked off "button" under traits but I'm not sure what to do after that. In Android, I would just do Object.setOnClickListener but as far as I know there is no method like that iOS. Any thoughts?
Create a tap gesture recogniser:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapHandler:)];
Add the tap gesture to the UIView:
[aView addGestureRecognizer:tap];
Then implement the tap handler:
- (IBAction)tapHandler:(UITapGestureRecognizer *)recognizer { // Handler code }
You should just be able to manually perform the required segue in the handler.
A simpler solution would be to implement the touchesBegan method as follows
This will detect when the view was clicked
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event