What is the best way to display a strikethrough animation? When a user swipes their finger across a UITableViewCell, I would like to animate a thin line across cell.textlabel.text
The two ways I've thought of so far would be using Animation or somehow displaying a custom image and revealing it slowly from left to right? Does anybody have any advice on this?
I already have the swipe gestures working, I now just need to know how to make the animation happen:
Add Gesture Recognizer:
//Add a left swipe gesture recognizer
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSwipeLeft:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];
//Add a right swipe gesture recognizer
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSwipeRight:)];
recognizer.delegate = self;
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.tableView addGestureRecognizer:recognizer];
Delegate Methods for Gestures:
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
NSLog(#"uncompleted");
}
// Cross Item off of the list
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
NSLog(#"completed");
}
Based on what you have so far the following should work:
when handleSwipeLeft (or right) fires, place a new UIView with a black background color over the textfield at the textfield's x point and around halfway to the y point with a width of 0 and height of 1
then, call [UIView animationWithDuration....] changing the UIView's width property to be roughly the width of the textfield.
This should be close to what you want with some tweaking. I don't think it will be possible to animate the strikethrough from using the properties of the font alone but this technique should simulate it just fine.
Good job on getting half way there.
Related
I want to implement a custom notification over to be shown from with in my app. It should show a message and allow user to swipe down to reveal couple of buttons for action. I am supporting minimum iOS 6.
By far, I am able to create a custom view subclassing UIView and put up message in it. How do I implement the feature of swipe down to reveal couple of buttons?
Any references would be helpful.
Try with this:
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandler:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self.view addGestureRecognizer:gestureRecognizer];
-(void)swipeHandler:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
}
Add a UIView below your notifications that will have a height of 0 and on swipe down, increase the height of that particular constraint.
I've made a custom UIView that appears in my app in two places. It first appears in a regular view controller along with a text view, and then a tap gesture on the UIView moves to another view controller where the custom UIView is shown enlarged. I want to have a different tap gesture for the enlarged UIView, but when I tried putting this into the storyboard, the first tap gesture stopped working (since the new tap gesture was only reachable via the first tap gesture's segue to the new view controller, I wasn't able to see if it worked or not.
Does adding the second UITapGestureRecognizer to the view cause the first to become invalid? I don't see any other reason this would've caused an issue. If so, do I need to create separate classes for the two different custom UIViews? I tried to avoid this since they're showing the same thing just in different sizes but it seems the different tap gestures may be causing an issue.
Adding different UITapGesture in different.
View1
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapBehind1:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO;
[View1 addGestureRecognizer:recognizer];
View2
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapBehind2:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO;
[View2 addGestureRecognizer:recognizer];
View1 handle Tap method
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
[View1 removeGestureRecognizer:sender];
}
}
View2 handle Tap method
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
[View2 removeGestureRecognizer:sender];
}
}
My collection view is working great. It shows a grid of photos and lists hundreds of them. You can swipe vertically to scroll through them all. Life is good. However, I now have a new requirement. I need to be able to detect when the user is swiping left or right. I need to be able to intercept this gesture so I can attach behavior to left and right swipes while keeping intact my collection view's vertical scroll capabilities. Any ideas?
You need two recognizers, one for swiping left, and the other for swiping right:
UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeftFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
and for the handler:
- (void)handleSwipeLeftFrom:(UIGestureRecognizer*)recognizer {
}
Finally, you add it to your view:
[view addGestureRecognizer:swipeUpGestureRecognizer];
The same for the other direction (just change all the Lefts to Rights).
add UISwipeGestureRecognizer to the cells settings its direction as below,
UISwipeGestureRecognizer *swipeRightDir = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeRightDirection:)];
swipeRightDir.delegate = self;
swipeRightDir.numberOfTouchesRequired = 1;
[swipeRightDir setDirection:UISwipeGestureRecognizerDirectionRight];
I have a small UITableView that is hidden when the view is loaded. When i click on "SHOW" UIButton, the UITableView is made visible by myTableView.hidden=NO;
I want to hide UITableView when a user touches outside its frame. Thanks for any help!
Best Approach
Simple.Before show up the UITable View add one more grayed out/Transparent view then add tap gesture recogniser on it to hide it . That's it.
Show Overlay View first - alpha will be 0.5f and background color should be clear color.
show the table view.
NOTE: over lay view should have tap recogniser which will hide the overlay and table view
in View did load
UITapGestureRecognizer *tapRecog = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(overlayDidTap:)];
[myOverLayView addGestureRecognizer:tapRecog];
- (void)overlayDidTap:(UITapGestureRecognizer *)gesture
{
//hide both overlay and table view here
}
Bad Approach
We should not add tap recogniser on main view itself. Because it may have lots of
controls inside of it. so when user tap on it. it will perform its operation. So to avoid
it we can simulate the same behaviour by above approach
You can get touch position by this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[self.view addGestureRecognizer:singleTap];
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:self.View];
}
Then just check if the point is in tableview frame. If not then hide the tableview. Hope this help. :)
Subclass UITableView, override pointInside:withEvent:. It is templated for this reason.
Something like:
-(BOOL)pointInside:(CGPoint) point withEvent:(UIEvent*) event
{
BOOL pointIsInsideTableView = [super pointInside:point withEvent:event];
BOOL pointIsOutsideTableView = // Some test
return pointIsInsideTableView || pointIsOutsideTableView;
}
So you can catch the touch in table view implementation, where it belongs logically in this case.
I have a scenario like shown below
Right now, I am showing only 1 view with label 8. But I am planning to add 3 more such views to the HolderView.
The SmallerView/s are created from other Nib files.
I did this code for adding Tap Recognizer for ViewController's view
UITapGestureRecognizer *tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapRecognized2:)];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];
Code for adding Tap Recognizer to the smaller views
I added smaller views to the HolderView. And assigned Tag IDs to them. After that,
for (SmallerView *view in HolderView.subviews) {
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[recognizer setDelegate:self];
NSLog(#"Added gesture to the view with tag: %ld",view.tag);
[view addGestureRecognizer:recognizer];
}
3.
- (void)tapRecognized:(UITapGestureRecognizer *)paramSender
{
NSLog(#"tapped on %ld", paramSender.view.tag);
}
- (void)tapRecognized2:(UITapGestureRecognizer *)paramSender
{
NSLog(#"VC view");
}
I have enabled UserInteraction (both in code and Inspector) for all the views and UILabels on smaller views too.
The problem now is...
The smaller view's Tap recognisers are not really working consistently. Sometimes they print the output. All at suddenly it prints the ViewController's recogniser's output.
Please help
UPDATE:
Below is my View diagram.
Green Border: (In UIView's initWithFrame)d
self.layer.borderColor = [UIColor greenColor].CGColor;
Red Border:
MyTile *tile = [[[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil] objectAtIndex:0];
self.myLabel.layer.borderColor=[UIColor redColor].CGColor;
Why that Green Border is coming only of that size? Shouldn't that be full square?
And, the gesture works only when I tap on the green area. Why?
It seems that you have a tap gesture recognizer on a view and also a tap gesture recognizer on its superview. The way gesture recognizers work is that, by default, both of them will be candidates to recognize the tap. If that is not what you want, it is up to you to prevent it.
You can:
set up a "you-go-first" relationship between the gesture recognizers, or
you can use their delegates to help decide between them, or
you can set up the subview so that it stops the superview's gesture recognizer from recognizing.
You have lots of options!
But if you do none of them, then both this view's gesture recognizer and its superview's gesture recognizer will be trying to recognize.
Perhaps a better solution would just to add one tapGestureRecognizer to the parent view.
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[tap addTarget:self action:#selector(handleTap:)];
[holderView addGestureRecognizer:tap];
Then add the target method
- (void) handleTap:(UITapGestureRecognizer *)tap {
UIView * holderView;
CGPoint tapPoint = [tap locationInView:holderView];
for (UIView * v in holderView.subviews) {
if (CGRectContainsPoint(v.frame, tapPoint)) {
// v is the subview that was pressed.
// add your code here.
break;
}
}
}
I have assigned a new frame to the frame which was having wrong borders. This helped in getting the gestures right.