UIWebView, enable touch hold save image, - webview

this is my code but not working
`- (void)webViewDidFinishLoad:(UIWebView *)webView
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
longPress.minimumPressDuration = 2;
[self.webview addGestureRecognizer:longPress];
[longPress release];
}
(void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(#"Long Press");
//do something
}
}`

Related

Adding both short long gestures in same view with proper differentiation

I am working on app in which I want both short long gesture in same view, I added but issue is I'm facing is that short gesture end always call, Need your help how to do that in right way. Below is my code.
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureOnFormFields:)];
longGesture.minimumPressDuration = 1.0f;
[longGesture setDelegate:self];
[self addGestureRecognizer:longGesture];
UILongPressGestureRecognizer *shortGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(shortGesture:)];
shortGesture.minimumPressDuration = 0.1f;
[shortGesture setDelegate:self];
[self addGestureRecognizer:shortGesture];
- (void)longGestureOnFormFields:(UIGestureRecognizer*) recogniser
{
if (recogniser.state == UIGestureRecognizerStateEnded ) {
}
- (void)shortGesture:(UIGestureRecognizer*) recogniser
{
if (recogniser.state == UIGestureRecognizerStateEnded ) {
}
You can manage that by using one UITapGestureRecognizer like,
self.view.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureOnFormFields:)];
longGesture.minimumPressDuration = 1.0f;
[longGesture setDelegate:self];
[self.view addGestureRecognizer:longGesture];
UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(testm)];
[self.view addGestureRecognizer:recog];
and your methods smething like,
-(void)testm{
NSLog(#"tap");
}
-(void)longGestureOnFormFields : (UILongPressGestureRecognizer*)sender{
if (sender.state == UIGestureRecognizerStateEnded ) {
NSLog(#"long press gesture");
}
}
I have added gesture recognizer in viewDidload i.e in viewcontroller so i have add it to self.view if you have subclass UIVIew and adding this to that view then add it on self like [self addgesture...] as shown in question!

Getting UIPanGesture and UISwipeGesture working together in the same direction

I've set up my UIView to call a method when the view is panned up and to call some other method when the view is swiped up. My pan works fine and I can keep my pan disabled if my velocity.y is above a certain limit, but I can never get the swipe action to work when my pan fails. I've tried playing around with the delegate methods without much luck. Looked over this solution but no luck: https://stackoverflow.com/a/21728621/1925859
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panRec.delegate = self;
[panedView addGestureRecognizer:panRec];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipeRec.delegate = self;
[panedView addGestureRecognizer:swipeRec];
[swipeRec requireGestureRecognizerToFail:panRec];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
if (ABS(velocity.y) > 200)
{
NSLog(#"Swipe actiavted");
return NO;
}
}
return YES;
}
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}
If you remove your delegate method, add direction to the swipe and change the receiver of requireGestureRecognizerToFail then it will work. Note that pans must be way slower than sweeps in order to be recognized.
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[panedView addGestureRecognizer:panRec];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
[panedView addGestureRecognizer:swipeRec];
[panRec requireGestureRecognizerToFail:swipeRec];
}
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}
- (IBAction)handlePan:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"PAN");
}

Move UITableView with UISwipeGestureRecognizer

I am trying to mix an UISwipeGestureRecognizer with an UITableView.
What I would like to do is, while I am doing the swipe gesture, move at the same time the UITableView outside the window and refresh the table data.
I am going to show you with images...
This is my view:
My View
And I would like to get something like this:
Desired View
I am able to move the table when the swipe gesture is ended, but not while I am doing the gesture, and that is what I want.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
swipeToRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToGroup:)];
[swipeToRight setDelegate:self];
[swipeToRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self table]addGestureRecognizer:swipeToRight];
swipeToLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToContact:)];
[swipeToLeft setDelegate:self];
[swipeToLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self table]addGestureRecognizer:swipeToLeft];
}
- (void)changeToGroup:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
- (void)changeToContact:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
I thought that I could do it with UIGestureRecognizerStateBegan, adding the animations inside that event, but I can't receive it...
Could anyone help me?
Thanks a lot!!.
Try this,
use UIPanGestureRecognizer
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan:)];
[recognizer setMaximumNumberOfTouches:1];
[recognizer setDelegate:self];
[[self table]addGestureRecognizer:recognizer];
and in
- (void) handlePan:(UIPanGestureRecognizer *)recognizer {
UIView *view = self.tableView;
if(recognizer.state == UIGestureRecognizerStateBegan) {
// remember where the pan started
//panGestureOrigin is a CGPoint variable
panGestureOrigin = view.frame.origin;
//optional to keep an enum to keep direction
//self.panDirection = MenuPanDirectionNone;
}
CGPoint translatedPoint = [recognizer translationInView:view];
if(translatedPoint.x > 20) {
//self.panDirection = MenuPanDirectionRight;
[self handleRightPan:recognizer];
} else if(translatedPoint.x < 0) {
//self.panDirection = MenuPanDirectionLeft;
[self handleLeftPan:recognizer];
}
}
- (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
//animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
}
- (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
// animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
}

detect from which recognizer a gesture is coming

I have 2 views: view with tag 1 and view with tag 2.
Each view has two gesture recognizers.
CGRect tempRect=CGRectMake(0, 0, 100, 100);
for (int i=1; i<=2; i++) {
UIView *tempView=[[UIView alloc] initWithFrame:tempRect];
UIImageView *tempImageView=[[UIImageView alloc]initWithFrame:tempRect];
tempImageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#%d%#",#"image_",i,#".png"]];
UISwipeGestureRecognizer *tempRecognizer = [[UISwipeGestureRecognizer alloc] init];
[tempRecognizer performSelector:#selector(SwipeRight:) withObject:[NSNumber numberWithInt:i]];
[tempRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[tempView addGestureRecognizer:tempRecognizer];
[tempView addSubview:tempImageView];
tempView.tag=i;
[self.view addSubview:tempView];
}
I have a method: SwipeRight that manages the swipe....
-(void)SwipeRight:(NSNumber*)MyTag{
int MyProgr = [MyTag intValue];
}
Which is the correct way to pass the tag?
With my code I got an error:
[UISwipeGestureRecognizer SwipeRight:]: unrecognized selector sent to instance...
Set the View Tag before
[tempView setTag:i];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(callYourMethod:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[tempView addGestureRecognizer:swipeRight];
- (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
int tagValue = recognizer.view.tag;
NSLog(#"Down");
}
}
Please try to use this one
-(void) SwipeRight:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
int tag = recognizer.view.tag;
}
}

Imitating button click with UIGestureRecognized

Having added a UITapGestureRecognized to a UIView, how do I parse the view for ie. changing the background color during the tap? The purpose of this is to imitate a button click.
UIView *locationView = [[UIView alloc] init];
locationView.tag = 11;
locationView.backgroundColor = [UIColor clearColor];
locationView.userInteractionEnabled = YES;
[locationView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(promptForLocation)]];
Let's say I want locationView.backgroundColor = [UIColor blueColor] right after the tap gesture. Would you just implement it in the target action or is there a specific implementation for this?
Update:
This is my final code inspired by #0x7fffffff
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetectedLocation:)];
longPress.allowableMovement = 50.0f;
longPress.minimumPressDuration = 0.05;
UILongPressGestureRecognizer *longPress2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetectedPhoto:)];
longPress2.allowableMovement = 50.0f;
longPress2.minimumPressDuration = 0.05;
[leftView addGestureRecognizer:longPress];
[rightView addGestureRecognizer:longPress2];
// ...
}
- (BOOL)longPressDetected:(UILongPressGestureRecognizer *)sender {
if ([self.view hasFirstResponder]) {
return NO;
}
if (sender.state == UIGestureRecognizerStateBegan) {
[sender.view setBackgroundColor:[UIColor colorWithRed:(4/255.0) green:(129/255.0) blue:(241/255.0) alpha:1]];
} else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) {
[sender.view setBackgroundColor:[UIColor clearColor]];
}
CGPoint location = [sender locationInView:sender.view];
return sender.state == UIGestureRecognizerStateEnded && location.x > 0 && location.x < sender.view.frame.size.width && location.y > 0 && location.y < sender.view.frame.size.height;
}
- (void)longPressDetectedLocation:(UILongPressGestureRecognizer *)sender {
if ([self longPressDetected:sender]) {
[self promptForLocation];
}
}
- (void)longPressDetectedPhoto:(UILongPressGestureRecognizer *)sender {
if ([self longPressDetected:sender]) {
[self promptForPhoto];
}
}
Considering you're trying to imitate a button click, I'm assuming you'd want the view to revert to its original state after the touch ends. To do this, you'll want to use a UILongPressGestureRecognizer instead of a UITapGestureRecognizer.
With a tap gesture, the recognizer isn't detected until the touch ends, so you'd effectively be highlighting the view as soon as you lift your finger. To get around this, use the long press gesture its minimumPressDuration property set to 0.0. Then in its selector, check the state of the sending gesture; If it has just begun, change the background color, and if it has ended revert back to the original color.
Here's an example:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)];
[myView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:myView];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetected:)];
[longPress setAllowableMovement:50.0f];
[longPress setMinimumPressDuration:0.0];
[myView addGestureRecognizer:longPress];
}
- (void)longPressDetected:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
[sender.view setBackgroundColor:[UIColor blueColor]];
}else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) {
[sender.view setBackgroundColor:[UIColor redColor]];
}
NSLog(#"%d",sender.state);
}
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedMethod:)];
[locationView addGestureRecognizer:gr];
This is method
-(void)tappedMethod:(UIGestureRecognizer *)ge
{
// write relavent code here;
locationView.backgroundColor = [UIColor blueColor];
}

Resources