Xcode keyboard issue on scroll view - objective-c

I have a scroll view on top single view. I have some textfields and UIPickers on it. Now I know how to make a keyboard go off when return is pushed. But, I am trying to get the keyboard off from textfield when the background is tapped or UIpicker is selected. I tried doing this...
Interface :
- (IBAction)textFieldReturn:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
Implementation :
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTouched:(id)sender
{
[textField resignFirstResponder];
}
But the problem is I cant make sroll view as control type to make it work..

Try like this may be it helps you but not sure,
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextField class]]) {
[yourtextfield resignFirstResponder];
}
}
And for getting touch event on scrollview you have to take geasture recognization,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTap)];
[scroll addGestureRecognizer:singleTap];
-(void)singleTap{
[text resignFirstResponder];
//write whatever you want it.
}

Related

how to add 3dtouchforce to UIButton?

I want to measure the touch force when tapping or dragging a button. i have created a UITapGestureRecognizer (for tapping) and added it to myButton like this:
UITapGestureRecognizer *tapRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(buttonPressed:)];
[tapRecognizer2 setNumberOfTapsRequired:1];
[tapRecognizer2 setDelegate:self];
[myButton addGestureRecognizer:tapRecognizer2];
i have created a method called buttonPrssed like this:
-(void)buttonPressed:(id)sender
{
[myButton touchesMoved:touches withEvent:event];
myButton = (UIButton *) sender;
UITouch *touch=[[event touchesForView:myButton] anyObject];
CGFloat force = touch.force;
forceString= [[NSString alloc] initWithFormat:#"%f", force];
NSLog(#"forceString in imagePressed is : %#", forceString);
}
I keep getting zero values (0.0000) for touch. any help or advice would be appreciated. i did a search and found DFContinuousForceTouchGestureRecongnizer sample project but found it too complicated. I use iPhone 6 Plus s that has touch. i can also measure touch when tapping on any other area in the screen but not on buttons using this code:
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
//CGFloat maximumPossibleForce = touch.maximumPossibleForce;
CGFloat force = touch.force;
forceString= [[NSString alloc] initWithFormat:#"%f", force];
NSLog(#"forceString is : %#", forceString);
}
You are getting 0.0000 in buttonPressed because the user already lifted his finger when this is called.
You are right, that you need to get the force in the touchesMoved method, but you need to get it in the UIButton's touchesMoved method. Because of that you need to subclass UIButton and override its touchesMoved method:
Header file:
#import <UIKit/UIKit.h>
#interface ForceButton : UIButton
#end
Implementation:
#import "ForceButton.h"
#implementation ForceButton
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGFloat force = touch.force;
CGFloat relativeForce = touch.force / touch.maximumPossibleForce;
NSLog(#"force: %f, relative force: %f", force, relativeForce);
}
#end
Also, there is no need to use a UITapGestureRecognizer to detect a single tap on a UIButton. Just use addTarget instead.

keyboard how to hide it

I have a UIView with some UITextField, but when I click at them, I cant hide the keyboard and the "send" button is behind the keyboard:
how can I hide it?
Thanks!
Add UITextField's delegate method in your code.Don't forget to set the delegate.Whenever return key is keyboard is pressed this below method will be called. Also set its delegates:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
EDIT : when once focused to textField and focus to other textfield, that textfield will not resign so do this as i assume from topmost u have textField1, textField2, textField3, textField4 if any more.... Add this delegate method.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == textField1) //motive resign other textFields
{
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField2) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField3 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField3) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField4) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
}
return YES;
}
EDIT U can use this also:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
return YES;
}
I use this code, this event runs when you tap anywhere in view, and you do not depend on textFields events:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([textField1 isFirstResponder] && [touch view] != textField1) {
[textField1 resignFirstResponder];
}
if ([textField2 isFirstResponder] && [touch view] != textField2) {
[textField2 resignFirstResponder];
}
...
[super touchesBegan:touches withEvent:event];
}

UIWebView addSubview:UIImageView not displaying when triggered by touch events

A new day, a new question!
I have been working on an app that requires I show a circle where the users finger(s) is(are) when they are touching the screen.
I have followed a tutorial I found on subclassing UIWindow, and my comment explains how to pass on those touches if you are using Storyboards and ARC.
I am using the following code (which is based on this tutorial) to make a touch indicator to display:
#pragma mark - Touch Events
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
// Enumerate over all the touches and draw a red dot on the screen where the touches were
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
// Get a single touch and it's location
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.webView];
// Add touch indicator
UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)];
[self.webView addSubview:touchView];
}];
NSLog(#"Touches began");
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
// Enumerate over all the touches and draw a red dot on the screen where the touches were
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
// Get a single touch and it's location
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.webView];
// Draw a red circle where the touch occurred
UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)];
[self.webView addSubview:touchView];
}];
NSLog(#"Touches moved");
}
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[UIView animateWithDuration:0.5f
animations:^{
view.alpha = 0.0;
}
completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
}
NSLog(#"Touches ended");
}
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
NSLog(#"Touches cancelled");
}
UserTouchImageView is a subclass of UIImageView, with the change to the default being:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.image = [UIImage imageNamed:#"greyCircle.png"];
self.alpha = 0.5f;
}
return self;
}
I've tested this code without a UIWebview (replacing the self.webView with self.view), and it works fine, drawing the circle where I click and drag, then fading out when I release the button.
I have also succeeded in instantiating the UserTouchImageView and adding it to the webView from the viewDidLoad method of the viewController.
As soon as I put the UIWebview in with the code above, the same line ([self.webView addSubview:touchView];) doesn't work. I still get the NSLog messages to the console, but the subview doesn't get visibly added.
Can anyone help me figure out why this doesn't appear? Is there any other code I need to be aware of, or any reason why I cannot do this?
Many thanks!
EDIT
I have uploaded my source so far here (MediaFire)
EDIT 2
I've added two methods as below:
-(void)listWebViewSubViews
{
NSLog(#"BEGIN LISTING SUBVIEWS");
for (UIView *view in [self.webView subviews]) {
NSLog(#"Subview: %#", view.description);
}
NSLog(#"END LISTING SUBVIEWS");
}
-(void)view:(UIView *)view addTouchIndicatorWithCentreAtPoint:(CGPoint)point
{
NSLog(#"View: %#, x: %f, y: %f", view.description, point.x, point.y);
// Add touch indicator
UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(point.x -15, point.y -15, 30, 30)];
[view addSubview:touchView];
}
These are called from two buttons outside the UIWebView, and from within the touchesbegan method:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.webView];
[self view:self.webView addTouchIndicatorWithCentreAtPoint:touchPoint];
[self listWebViewSubViews];
}];
NSLog(#"Touches began");
}
The logging seems to indicate that the webview referenced by self.webView WITHIN the touches began method is an entirely seperate instance than the one when referenced from the two buttons. I can add multiple subviews via the button, and then list them, and they all show up in the logging, yet when I click to touch on the simulator, none of these extra subviews get listed.
Does anyone know of any special functionality in a UIWebView to have a duplicate instance on touch events?
A UIWebView is not one view but a collection of views. When you add a subview to a UIWebView the new subview is added to a hierarchy of subviews. Your new subview is probably being added behind other opaque views. It's there, but hidden. I'd recommend adding this indicator view to a different view in the hierarchy.
The default value of userInteractionEnabled for UIImageView is NO. You may need to set it to YES.
Problem solved:
Unfortunately this was caused by somethign entirely different, so thank you everyone for the help you've given.
The problem for this arose from my attempt to fix the code to run with storyboards. I had tried to access the view controller from the storyboard, and add that as the view to receive touch events as a priority (see the tutorial I linked to, and my comment, which I have also responded to). Where as I should have been using self.window.rootViewController.
My updated comment is here

UIScrolllView and info button

I have custom UIViewController which looks like this.
#implementation UIViewControllerCustom
- (id)init
{
self = [super init];
if (self) {
NSLog(#"init");
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton setFrame:CGRectMake(290, 10, 16, 16)];
[infoButton addTarget:self action:#selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoButton];
}
-(void)showInfo
{
About *about = [[About alloc]init];
NSLog(#"touched");
[about setModalTransitionStyle:UIModalPresentationFullScreen];
[self presentModalViewController:about animated:YES];
}
#end
Any view controller in my app inherits this controller. When info button is touched the modal window is present. The problem is that one of my view controllers use UIScrollView. In this controller info button is visible and it reacts on touches. The problem is that when I touch the button showInfo method of UIViewControllerCustom is not called. I have no such a problem in the other controller.
What can be wrong here ?
Because the UIScrollView handles the message being sent. In your case the touch on the button will be handled by the UIScrollView.
I would suggest you to subclass the UIScrollView and implement: - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view to determine if the UIScrollView should handle the event or not.
The implementation could be something along the lines of:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
if ([view isKindOfClass:[UIButton class]]) {
return NO;
}
return YES;
}

Unable to push the VIEW

I've a DashBoard View which has labels and imageViews drawn on it.
In my application, i want to push the a view which is in StaticPage.h when I touch any ImageView.
I used the concept of UITouch which has method "- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event" as follows:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
NSLog(#"touches began");
if ( [touch view] == _icon) {
StaticPage *staticPage = [[StaticPage alloc] initWithNibName:#"StaticPage" bundle:nil];
[self.navigationController pushViewController:staticPage animated:YES];
[staticPage release];
}
}
where the StaticPage which inherits from UIViewController.
_icon is a UIImageView.
In the AppDelegate, this method "- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions" is as follows:
self.window.rootViewController = self.viewController;
[_window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
where navController is declared in .h file of StaticPage as UINavigationController *navController; and synthesized in .m file of StaticPage.
I wonder why my view from StaticPage is not getting pushed??
Any Help... Thank You in Advance
I figured out the navigation but not using method pushViewController:animated.
Using presentModalViewController:animated we can navigate to the desired page.