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;
Related
I have a Scrolleview and i had its container View a UIView and UIVew contains textfields but Keyboard is not Poping..
I had use this methods...
-(void)scrollViewAdaptForEditingtext:(UITextField*)textfield {
CGPoint point = CGPointMake(0, textfield.frame.origin.y - 1.5 * textfield.frame.size.height);
[self.scrollView setContentOffset:point animated:YES];
}
-(void)scrollViewEndEditingtext:(UITextField*)textfield {
CGPoint point = CGPointMake(0, 0);
[self.scrollView setContentOffset:point animated:YES];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self scrollViewAdaptForEditingtext:textField];
return YES;
}
#pragma TextFieldDelegateProtocol
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.txtFieldFirstName) {
[self.txtFieldLastName becomeFirstResponder];
} else if (textField == self.txtFieldLastName) {
[self.txtFieldEmail becomeFirstResponder];
} else if (textField == self.txtFieldEmail) {
[self.txtFieldMobNumber becomeFirstResponder];
} else if (textField == self.txtFieldMobNumber) {
[self.txtFieldGender becomeFirstResponder];
} else
[textField resignFirstResponder];
[self scrollViewEndEditingtext:textField];
return YES;
}
can I include uiview also
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 !
I am using JTRevealSideBarDemoV2 for the implementation of the side panels like facebook and Path application. I have configured it in every sense and just want to add one more feature into this library. What I want is to apply gesture recognizer control in this library just similar to facebook. can somebody help?
The link for downloading this library is:-https://www.cocoacontrols.com/controls/jtrevealsidebar
Thanks!
#wattson:- this is the code which I have added for implementing Gesture Recognizer. BBut it is behaving very abnormally. If you have downloaded the code for JTRevealSideBarDemoV2 then you can analyze the code which i have written for.
The code is:
(void)setUpGestures{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(movePanel:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self.view addGestureRecognizer:panRecognizer];
}
-(void)movePanel:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender
translationInView:self.view];
CGPoint velocity = [(UIPanGestureRecognizer*)sender velocityInView:[sender view]];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
//UIView *childView = nil;
if(velocity.x > 0) {
if (!JTRevealedStateRight) {
[self revealLeftSidebar:(UIPanGestureRecognizer *)sender];
}
} else {
if (!JTRevealedStateLeft) {
[self revealRightSidebar:(UIPanGestureRecognizer *)sender];
}
}
// Make sure the view you're working with is front and center.
//[self.view sendSubviewToBack:];
[[sender view] bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
}
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
if(velocity.x > 0) {
// NSLog(#"gesture went right");
} else {
// NSLog(#"gesture went left");
}
if (!_showPanel) {
[self revealLeftSidebar:(UIPanGestureRecognizer *)sender];
} else {
if (JTRevealedStateLeft) {
[self revealRightSidebar:(UIPanGestureRecognizer *)sender];
} else if (JTRevealedStateRight) {
[self revealLeftSidebar:(UIPanGestureRecognizer *)sender];
}
}
}
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) {
if(velocity.x > 0) {
// NSLog(#"gesture went right");
} else {
// NSLog(#"gesture went left");
}
// Are you more than halfway? If so, show the panel when done dragging by setting this
value to YES (1).
_showPanel = abs([sender view].center.x - self.view.frame.size.width/2) >
self.view.frame.size.width/2;
// Allow dragging only in x-coordinates by only updating the x-coordinate with
translation position.
[sender view].center = CGPointMake([sender view].center.x + translatedPoint.x, [sender
view].center.y);
[(UIPanGestureRecognizer*)sender setTranslation:CGPointMake(0,0) inView:self.view];
// If you needed to check for a change in direction, you could use this code to do so.
if(velocity.x*_preVelocity.x + velocity.y*_preVelocity.y > 0) {
// NSLog(#"same direction");
} else {
// NSLog(#"opposite direction");
}
_preVelocity = velocity;
}
}
Original question:
I have 3 UITextFields (nameField, locationField, and discriptionField) I need to have a if statement trigger on discriptionField only, however the way I have tried it (configured below) all 3 textfields perform setViewMovedUp. I have also tried
if([sender isEqual: discriptionField])
but I'm getting the same problem, all 3 textfields preform the method.
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if (sender == discriptionField)
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
My Solution:
Beginner mistake, I was calling that same method from another method without realizing it. Probably a poor way to solve the problem but here is my solution.
BOOL onlyDiscription = NO;
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:discriptionField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
onlyDiscription = YES;
}
}
}
-(void)keyboardWillShow {
if (onlyDiscription) {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
}
-(void)keyboardWillHide {
if (onlyDiscription) {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
con = YES;
[self setViewMovedUp:NO];
}
onlyDiscription = NO;
}
}
Better to use tag value and compare with tag value.
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if (sender.tag == discriptionField.tag)
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
(or)
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if([sender isEqual:discriptionField]){
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
Please check like this
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:discriptionField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
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;
}