how to detect touch on UIView with UIimage ontop of it? - iphone

i want to use a UIview and a UIImage ontop of it with a text box and button i want the user to drag it when the UIview or anything in the view is touched, what would be the best way to implement this method?
the UIView is on a single View Controller and using Interface Builder to make my UIviews and UIimages.

Have a look at the Apple Sample "MoveMe" http://developer.apple.com/iphone/library/samplecode/MoveMe/Introduction/Intro.html
The class MoveMeView in there is draggable. (There's also some superfluous animation going on, but the dragging is there).

You need to subclass UIView and implement
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event, and
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event.

Related

Shake for random XIB on iPhone

I have a bunch of XIB files and I would like it so if the user shakes the device, one of the XIBs are loaded onto the screen and I was wondering how I could go about doing this.
Thanks a lot in advance!
Daniel
There are 3 methods you can use
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
I use
-(void)motionEnded:(UIEventSubtype)motion
withEvent:(UIEvent *)event
{
// show your view
[self presentModalViewController:yourView animated:YES];
}
If you want to handle more than 1 shake event check out How do I detect when someone shakes an iPhone?

how to "bypass" touch events in Cocoa-touch?

I want to make a transparent mask-view over the current window, which just tracks touch events and passing them to the visible views below. However if I set userInteractionEnabled=YES to this mask, this blocks the events and won't be passed below.
Is there any way that I can prevent this view from blocking the events, or manually passing the events below?
Thanks,
I just recently did this for one of my apps and it turned out to be quite simple.
Get ready to subclass UIView:
I called my Mask View the catcher view and this is how the protocol looks:
#interface CatcherView : UIView {
UIView *viewBelow;
}
#property(nonatomic,retain)UIView *viewBelow;
#end
Here you are just subclassing UIView AND keeping a reference to the view bellow.
On the implementation you need to fully implement at least 4 methods to pass the touches to the view, or views bellow, this is how the methods look:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Began");
[self.viewBelow touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Moved");
[self.viewBelow touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Ended");
[self.viewBelow touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Cancelled");
//Not necessary for my app but you just need to forward it to your view bellow.
}
Just remember to set the view or views that are bellow when you create the view; it is also very important to set the background color to clear, so it acts as a mask. THis is how that looks:
CatcherView *catchView=[[CatcherView alloc] initWithFrame:[self.view bounds]];
catchView.backgroundColor=[UIColor clearColor];
catchView.viewBelow=myViewBellow;
[self.view addSubview:catchView];
Hope it helps and comment if you need more info.
UIKit determines the target view for an event by sending -hitTest:withEvent: messages down the responder chain
Once the target has been found, the event is sent up the responder chain until a responder that handles it is found (often the view that was touched, but not always)
Thus, if you override -[NSView hitTest:withEvent:] in a suitably high up view (perhaps by using a custom window!) you can note all incoming events and call super to have them behave as normal.

How to enable touch began in UIScrollView?

Am using UIScrollview, when i am trying to capture touch event in UIscrollview is not responding. how should i achieve it? i am using UILable for showing the text.
Yes I was also facing the same problem.But finally I got remedy on it.
Solution is that you will have to create a class inherited from UIScrollview as follows:
#import <Foundation/Foundation.h>
#interface CustomScrollView : UIScrollView
{
}
#end
And override the touches method in its implementation file as follows:
#import "CustomScrollView.h"
#implementation CustomScrollView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if(!self.dragging){
[self.nextResponder touchesMoved:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self.nextResponder touchesEnded:touches withEvent:event];
}
#end
You will have to import this class in the required file and use its object instead of UIScrollview"s object.
Now you can see that control reaches to touches methods in the class in which you have added scrollview whenever you touch on it.
Thanks.
As of iOS 3.2 UIScrollView does not listen to touchesBegan:, touchesMoved:, touchesEnded:, etc. anymore, afaik.
You should add Gesture Recognizers instead to handle touch events in your UIScrollView.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
But if you want to handle the touches in one of the UIScrollView's subviews, it would be best to read the Overview section in the UIScrollView documentation. See touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and delaysContentTouches, among others.
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html
"split" writes about a solution here that I used in my project to be able to access touches in a scroll view.
No need to go anywhere simple solution is here. Really as simple as you think.

UIWebView with Touch Event

I need to be able to catch touch events on a UIWebView and still be able to use the webview's contents (html links, etc...)
I have subclassed UIWebView and implemented methods:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
I am now able to catch touch events, but the view has become ususable :(
I then tried to set the following on the webview object in my UIViewController, but that changed nothing.
[webview setDelegate:self];
[webview setUserInteractionEnabled:YES];
Can anyone help? Do you need further info?
Tia,
S.
I ended up not subclassing UIWebView and using a Gesture Recognizer on the object in the view controller. It works really well!
S.
In each of the functions you implemented did you also call super? For instance
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Your code here
[super touchesBegan:touches withEvent:event];
}
If don't call super UIWebView can't know the someone touched a link on it

How to enable user to click the View to hide the kb on iPhone?

I find that the View do not have a touch inside or similar event. So, I make a button on top on the view, and set it Alpha to 0, but after I set to Alpha 0, it cannot touch anymore. Any ideas on this?
You can use UIView subclass and handle touch event there. Methods to look at (defined in UIResponder):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
If you want to use "invisible" UIButton you should set its type to UIButtonTypeCustom - by default it will appear with no image and title and with transparent background. So technically it will be invisible to user but still be able to respond to all events.