Shake for random XIB on iPhone - 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?

Related

Can One touch handled by Two UIViews?

Say, I've two overlapping UIView layers:
----view-top
--view-below
When I touch the screen, I want the touch be handled by these two views, not just one.
How can I do that?
Try this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesBegan:touches withEvent:event];
}
This will send the touch to the next responder in the chain, in this case, your view!

ONTouch event issues?

Touching anywhere on screen the user should go to the next view in iphone application?
How can we implement this?
Sure you can Make use of this method of touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get all the touches.
NSSet *allTouches = [event allTouches];
//Number of touches on the screen
if([allTouches count] > 0)
{
//Navigate to next screen here likewise you generally do
}
else
{
}
}
Good Luck!
Read UIResponder Class Reference for more details
For now you can implement the
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method in your viewController class and load the otherViewController possibly by pushing over NavigationController.

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 detect touch on UIView with UIimage ontop of it?

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.

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.