I have several custom buttons in my view and made them a custom focus animation with didUpdateFocusInContext ,and I added a UITapGestureRecognizer to the view that when user taps twice a method will run ! but the problem is when didUpdateFocusInContext prevents to double tap action until the focused context reaches its end (it means riches to the last button) then method will fire when there is no focusable context !!! how can prevent such thing ? Here is my code :
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator: (UIFocusAnimationCoordinator *)coordinator {
//Setup focausing for main buttons
for (UIButton *mainButton in _nextPrevButton){
if (context.nextFocusedView == mainButton) {
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.40 initialSpringVelocity:.60 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
context.nextFocusedView.transform = CGAffineTransformMakeScale(1.2, 1.2);
context.nextFocusedView.layer.shadowOffset = CGSizeMake(0, 0);
context.nextFocusedView.layer.shadowOpacity = 1;
context.nextFocusedView.layer.shadowRadius = 15;
context.nextFocusedView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
context.nextFocusedView.layer.shadowOpacity = 1;
} completion:nil];
} else {
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.40 initialSpringVelocity:.60 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
context.previouslyFocusedView.transform = CGAffineTransformMakeScale(1, 1);
context.previouslyFocusedView.layer.shadowOpacity = 0;
} completion:nil];
}
}
if (context.nextFocusedView == _button1Focused) { [self buttonFocused:context]; } else if (context.previouslyFocusedView == _button1Focused) { [self buttonNotFocused:context]; }
if (context.nextFocusedView == _button2Focused) { [self buttonFocused:context]; } else if (context.previouslyFocusedView == _button2Focused) { [self buttonNotFocused:context]; }
if (context.nextFocusedView == _button3Focused) { [self buttonFocused:context]; } else if (context.previouslyFocusedView == _button3Focused) { [self buttonNotFocused:context]; }
}
Tapping methods :
- (void)viewDidLoad {
UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightTap:)];
[rightTap setAllowedPressTypes:#[#(UIPressTypeRightArrow)]];
[rightTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:rightTap];
}
- (void)handleRightTap:(UITapGestureRecognizer *)sender {
//or put the code here !
if (sender.state == UIGestureRecognizerStateRecognized)
{
NSLog(#"Right Arrow");
}
}
I tried other states :
if (sender.state == UIGestureRecognizerStateBegan)
{
// handling code
NSLog(#"UIGestureRecognizerStateBegan ");
}
if (sender.state == UIGestureRecognizerStateEnded)
{
// handling code
NSLog(#" UIGestureRecognizerStateEnded ");
}
if (sender.state == UIGestureRecognizerStateRecognized)
{
// handling code
NSLog(#"UIGestureRecognizerStateRecognized");
}
but no success !
Related
I have 13 text fields. I want to set view animation for only 4 text fields which will be displayed in bottom
Here is my code:
if (textField.editing != ((_GET_companyname)||(_GET_firstname)||(_GET_middlename)||(_GET_lastname)||(_GET_companyurl)||(_GET_streetaddress)||(_GET_postbox)||(_GET_code)||(_GET_phonenum)))
{
NSLog(#"Entered in condition");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//Keyboard becomes visible
[UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.25];
self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
[UIView commitAnimations];
}
}
else
{
NSLog(#"Entered else Part");
}
I need to set the below to all 14 textfields
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
According to you if "_GET_companyname" is a IBOutlet of UITextField.
Then you should write something like this below
if ((textField != _GET_companyname)&&(textField != _GET_firstname)) // etc
{
NSLog(#"Entered in condition");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//Keyboard becomes visible
[UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.25];
self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
[UIView commitAnimations];
}
}
else
{
NSLog(#"Entered else Part");
}
Edit :- And also instead of checking (!=). you should check (==) for few UITextFields. That will be better.
You can check these using tags
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textFiledName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledName.tag = 0;
textFiledName.delegate = self;
[self.view addSubview:textFiledName];
UITextField *textFiledLastName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledLastName.tag = 1;
textFiledLastName.delegate = self;
[self.view addSubview:textFiledLastName];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.tag == 0) {
//No Animation
}else if (textField.tag == 1){
//Add Animation
}
return YES;
}
How can I do it with out resize view frame (or tableView)?
I use this code and I have resized view then I scroll down/up
I use code, not storyboard
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer class] == [UIPanGestureRecognizer class])
{
UIPanGestureRecognizer *panGestureRec = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint distance = [panGestureRec translationInView:self.tableView];
if (distance.y > 0 || distance.y < 0)
{
if (distance.y > 0) // down
{
//NSLog(#"user swiped down");
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (distance.y < 0) //up
{
//NSLog(#"user swiped up");
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
return NO;
} else {
return YES;
}
}
return YES;
}
Try this code . This will give smooth animation (Used UIView Animation property)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer class] == [UIPanGestureRecognizer class])
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
UIPanGestureRecognizer *panGestureRec = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint distance = [panGestureRec translationInView:self.tableView];
if (distance.y > 0 || distance.y < 0)
{
if (distance.y > 0) // down
{
//NSLog(#"user swiped down");
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (distance.y < 0) //up
{
//NSLog(#"user swiped up");
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
return NO;
} else {
return YES;
}
[UIView commitAnimations];
}
return YES;
I am implementing a custom focus bar on top of my keyboard. The focus bar has widgets to move cursor back and forward along with a done button.
Now, with iOS 7, I am seeing the focus bar is moving faster than keyboard. Because of this for a second I see screen behind the focus bar before it sits on top of keyboard. This is working fine in iOS 6.
Below is what I am doing:
- (void)keyboardWillShow:(NSNotification *)iNotification {
self.dismissForm = NO;
self.shouldScrollCell = YES;
CGFloat aKeyboardAnimationDuration = [[iNotification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect aKeyboardFrame = [[iNotification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect adjustedKeyboardFrame = [self.view convertRect:aKeyboardFrame fromView:nil];
CGFloat adjustedHeight;
adjustedHeight = aKeyboardFrame.size.height + self.focusControlBar.frame.size.height;
[self.focusControlBar viewForFocusControlWillShowWithEndFrame:adjustedKeyboardFrame andAnimationDuration:aKeyboardAnimationDuration];
[UIView animateWithDuration:0.3 animations:^() {
[self.tableView setContentInset:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
}];
}
This is my focus bar animation code:
- (void)viewForFocusControlWillShowWithEndFrame:(CGRect)iFrame andAnimationDuration:(CGFloat)iDuration {
BOOL aShouldAppear = YES;
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarShouldAppear:)]) {
aShouldAppear = [self.delegate focusControlBarShouldAppear:self];
}
if (aShouldAppear) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarWillAppear:)]) {
[self.delegate focusControlBarWillAppear:self];
}
CGRect aBarFrame = self.frame;
UIInterfaceOrientation anInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
aBarFrame.size.height = [self heightForOrientation:anInterfaceOrientation];
self.frame = aBarFrame;
aBarFrame.origin.y = self.superview.bounds.size.height - iFrame.size.height - aBarFrame.size.height;
[UIView animateWithDuration:iDuration animations:^(void) {
self.frame = aBarFrame;
} completion:^(BOOL iFinished) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarDidAppear:)]) {
[self.delegate focusControlBarDidAppear:self];
}
}];
}
}
I just got to know that with iOS 7 we need to also keep KeyboardAnimationCurve into account. With the below modified code I had it worked.
- (void)keyboardWillShow:(NSNotification *)iNotification {
self.dismissForm = NO;
self.shouldScrollCell = YES;
NSDictionary *aNotificationInfo = [iNotification userInfo];
CGFloat aKeyboardAnimationDuration = [aNotificationInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve aKeyboardAnimationCurve = [aNotificationInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect aKeyboardFrame = [aNotificationInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect adjustedKeyboardFrame = [self.view convertRect:aKeyboardFrame fromView:nil];
CGFloat adjustedHeight;
adjustedHeight = aKeyboardFrame.size.height + self.focusControlBar.frame.size.height;
[self.focusControlBar viewForFocusControlWillShowWithEndFrame:adjustedKeyboardFrame animationCurve:aKeyboardAnimationCurve andAnimationDuration:aKeyboardAnimationDuration];
[UIView animateWithDuration:0.3 animations:^() {
[self.tableView setContentInset:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
}];
}
- (void)viewForFocusControlWillShowWithEndFrame:(CGRect)iFrame animationCurve:(UIViewAnimationCurve)iAnimationCurve andAnimationDuration:(CGFloat)iDuration {
BOOL aShouldAppear = YES;
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarShouldAppear:)]) {
aShouldAppear = [self.delegate focusControlBarShouldAppear:self];
}
if (aShouldAppear) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarWillAppear:)]) {
[self.delegate focusControlBarWillAppear:self];
}
CGRect aBarFrame = self.frame;
UIInterfaceOrientation anInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
aBarFrame.size.height = [self heightForOrientation:anInterfaceOrientation];
self.frame = aBarFrame;
aBarFrame.origin.y = self.superview.bounds.size.height - iFrame.size.height - aBarFrame.size.height;
[UIView animateWithDuration:iDuration delay:0.0 options:(iAnimationCurve << 16) animations:^{
self.frame = aBarFrame;
} completion:^(BOOL finished) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarDidAppear:)]) {
[self.delegate focusControlBarDidAppear:self];
}
}];
}
}
In Apple iOs photos app, Each picture take the full screen, but when You tap on it, navigation bar and tab bar with some menu options (like share picture) just appear and remain for a couple of secconds. How can I do that in my UIImageView ?
Add a UITapGestureRecognizer to your view and UiView for your topbar and bottom bar or what else you like and follow below code. I think This may help you.
//Write below code in ViewDidLoad
UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
[self.view addGestureRecognizer:singleTapOne]; [singleTapOne release];
for (UIGestureRecognizer *gR in self.view.gestureRecognizers) {
gR.delegate = self;
// handleSingleTap Method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateRecognized)
{
CGRect viewRect = recognizer.view.bounds; // View bounds
CGPoint point = [recognizer locationInView:recognizer.view];
CGRect areaRect = CGRectInset(viewRect, TAP_AREA_SIZE, 0.0f); // Area
if (CGRectContainsPoint(areaRect, point)) // Single tap is inside the area
{
if ((m_CtrlViewTopBar.hidden == YES) || (m_CtrlViewBottomBar.hidden == YES))
{
[self showToolbar:m_CtrlViewTopBar];
[self showToolbar:m_CtrlViewBottomBar]; // Show
}
else
{
[self hideToolbar:m_CtrlViewTopBar];
[self hideToolbar:m_CtrlViewBottomBar];
}
return;
}
CGRect nextPageRect = viewRect;
nextPageRect.size.width = TAP_AREA_SIZE;
nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
if (CGRectContainsPoint(nextPageRect, point)) // page++ area
{
//[self incrementPageNumber]; return;
}
CGRect prevPageRect = viewRect;
prevPageRect.size.width = TAP_AREA_SIZE;
if (CGRectContainsPoint(prevPageRect, point)) // page-- area
{
//[self decrementPageNumber]; return;
}
}
}
- (void)hideToolbar:(UIView*)view //Hide Toolbars
{
#ifdef DEBUGX
NSLog(#"%s", __FUNCTION__);
#endif
if (view.hidden == NO)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
view.alpha = 0.0f;
}
completion:^(BOOL finished)
{
view.hidden = YES;
}
];
}
[timer invalidate];
timer=nil;
}
- (void)showToolbar:(UIView*)view //Show Toolbars
{
#ifdef DEBUGX
NSLog(#"%s", __FUNCTION__);
#endif
if (view.hidden == YES)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
view.hidden = NO;
view.alpha = 1.0f;
}
completion:NULL
];
if (!timer) {
timer=[NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:#selector(HideToolBarWithTime)
userInfo:nil
repeats:YES];
}
}
[self.view addSubview:view];
}
-(void)HideToolBarWithTime //Hide Toolbars with time
{
[self hideToolbar:m_CtrlViewTopBar];
[self hideToolbar:m_CtrlViewBottomBar];
[timer invalidate];
timer=nil;
}
// Gesture Delegates
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
I want to hide my toolbar if I click on a button, but it doesn't seem to work. I can hide the toolbar with an animation but I can't let it show up again. Thanks!
- (IBAction)hideTheToolBar{
//[toolBar setHidden:YES];
if (toolbar.hidden == NO)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
toolbar.alpha = 0.0f;
}
completion:^(BOOL finished)
{
toolbar.hidden = YES;
}
];
}
if (toolbar.hidden == YES) {
toolbar.hidden = NO;
}
}
i think u are missing a statement in the second if loop try:
if (toolbar.hidden == YES) {
toolbar.hidden = NO;
toolbar.alpha = 1.0f ;
}