Detecting position tapped with UITapGestureRecognizer - objective-c

Hi so I was wondering if there was any way possible to get the position that was touched using UITapGestureRecognizer to recognize taps on the background.
-(void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized)
{
if ( bomb == nil)
{
bomb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bomb.png"]];
bomb.center = CGPointMake(10, 1);
bomb.frame = CGRectMake(113,123,67,67);
[self.view addSubview:bomb];
//[self performSelector:#selector(Update) withObject:nil afterDelay:1];
NSLog(#"Spawned bomb");
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(dropBomb) userInfo:nil repeats:YES];
}
}
}

Sure there is! Use locationInView: to get the CGPoint location of the touch on your image view.
CGPoint touchLocation = [sender locationInView:sender.view];
Or, if you are allowing multiple touches, you can use the following to specify which touch you are interested in.
CGPoint otherTouchLocation = [sender locationOfTouch:0 inView:sender.view];

Related

UILabel to appear for 3 seconds after UIButton is pressed

After I hit a pause button, I want a label to appear until the user taps on the screen. How do I do this?
So far I have this
- (IBAction)ButtonPausePressed:(id)sender {
PauseLabel.hidden = false (//how do i make it only visible until user taps?//)
if (GameEnd != true){
if ([GameUpdate isValid]){
[GameUpdate invalidate];
[BirdUpdate invalidate];
}else{
BirdUpdate = [NSTimer scheduledTimerWithTimeInterval:0.015
target:self
selector:#selector(UpdateBird)
userInfo:nil
repeats:YES];
GameUpdate = [NSTimer scheduledTimerWithTimeInterval:0.025
target:self
selector:#selector(GameUpdate)
userInfo:nil
repeats:YES];
}
}
}
- (IBAction)ButtonPausePressed:(id)sender {
PauseLabel.hidden = false
[self performSelector:#selector(hiddenLabel) withObject:nil afterDelay:3];
...
}
- (void)hiddenLabel{
PauseLabel.hidden = YES;
}
Try following
[your_view addSubview:your_label];
your_label.hidden = YES;
[your_label performSelector:#selector(setHidden:) withObject:#NO afterDelay:3];
Try like this:
-(void)viewDidLoad
{
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(btnSingleClicked:)];
[singleTapGesture setDelegate:self];
singleTapGesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTapGesture];
}
-(void)btnSingleClicked:(UITapGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateEnded && !self.PauseLabel.hidden)
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:3.0f // 3 sec to hide it
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.PauseLabel setAlpha:0.0f];
}
completion:^(BOOL finished)
{
// remove from super view if needed.
// Now you can handle touch events etc
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}
The idea behind the line is add a single tap gesture on your viewcontroller's view and if the pause label is displayed and a touch is happend on view check its state is ended and hide label with animation.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
PauseLabel.hidden = YES;
});

How to elegantly transition images in slide show?

After some research on here I found a solution to creating a slide show of images in an iphone app. All working fine, currently the images show one after another.
My question is, can I make the images cross dissolve/fade rather than just appearing, and if so could I get some advice on that.
My Code
.m
}
int topIndex = 0, prevTopIndex = 1;
- (void)viewDidLoad
{
imagebottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagebottom];
imagetop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagetop];
imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"ip2.png"],
nil];
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
-(void)onTimer{
if(topIndex %2 == 0){
[UIView animateWithDuration:5.0 animations:^
{
imagebottom.alpha = 0.0;
}];
imagetop.image = [imageArray objectAtIndex:prevTopIndex];
imagetop.image = [imageArray objectAtIndex:topIndex];
}else{
[UIView animateWithDuration:5.0 animations:^
{
imagetop.alpha = 1.0;
}];
imagetop.image = [imageArray objectAtIndex:topIndex];
imagebottom.image = [imageArray objectAtIndex:prevTopIndex];
}
prevTopIndex = topIndex;
if(topIndex == [imageArray count]-1){
topIndex = 0;
}else{
topIndex++;
}
You have a bunch of options. If you have a "container" view, you can make a new UIImageView transparent (alpha = 0), then use a UIView animation block to fade one image in and the other out (or leave alpha = 1 on both and slide one in from any side you want.
For instance, you have your main view, self.view. You have one UIImageView *oldView, that is now at rect (0,0,320,100) and you want to slide it to the right as you slide a newView imageView in. First you set the newView frame to (-320,0,320,100) then [self.view addSubview newView]. To animate the change:
[UIView animateWithDuration:2 animations:^
{
oldView.frame = CGRectMake(320, 0, 320, 100);
newView.frame = CGRectMake(0,0,320, 100);
}
completion:^(BOOL finished)
{
[oldView removeFromSuperView];
} ];
You also have the option of using UiView's
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
which gives you more/different options (and its less work too!). For instance, using the same basic objects in the first example, but the newView having the same frame as the oldView:
transitionFromView:oldView toView:newView duration:2 options: UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished)) { /*whatever*/}];

Increasing Velocity

Hey everyone I am making an app where you're a blue ball and have to move without letting the red ball touch you and I was wondering what could I do so that every second the velocity increases I have something like this:
- (void)viewDidLoad {
[super viewDidLoad];
//(X Speed, Y Speed)
pos = CGPointMake(13.0,8.0);
pos2 = CGPointMake(10.0,9.0);
}
- (IBAction)restart:(id)sender {
[restartbutton setHidden:YES];
[menubutton setHidden:YES];
randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
- (IBAction)start:(id)sender {
[startbutton setHidden:YES];
[restartbutton setHidden:YES];
[menubutton setHidden:YES];
randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
- (IBAction)menu:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)onTimer {
[self checkCollision];
enemy.center = CGPointMake(enemy.center.x+pos.x, enemy.center.y+pos.y);
enemy2.center = CGPointMake(enemy2.center.x+pos2.x, enemy2.center.y+pos2.y);
if (enemy.center.x > 320 || enemy.center.x < 0)
pos.x = -pos.x;
if (enemy.center.y > 480 || enemy.center.y < 0)
pos.y = -pos.y;
if (enemy2.center.x > 320 || enemy2.center.x < 0)
pos2.x = -pos2.x;
if (enemy2.center.y > 480 || enemy2.center.y < 0)
pos2.y = -pos2.y;
}
- (void)checkCollision {
if (CGRectIntersectsRect(player.frame, enemy.frame)) {
[randomMain invalidate];
[startbutton setHidden:YES];
[restartbutton setHidden:NO];
[menubutton setHidden:NO];
CGRect frame = [player frame];
frame.origin.x = 135.0f;
frame.origin.y = 205.0f;
[player setFrame:frame];
CGRect frame2 = [enemy frame];
frame2.origin.x = 135.0f;
frame2.origin.y = 20.0f;
[enemy setFrame:frame2];
CGRect frame3 = [enemy2 frame];
frame3.origin.x = 135.0f;
frame3.origin.y = 390.0f;
[enemy2 setFrame:frame3];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lost!"
message:[NSString stringWithFormat:#"Better Luck Next Time"]
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
player.center = [myTouch locationInView:self.view];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
#end
It seems that the only method you've given us that is being constantly called is onTimer. Why don't you set an acceleration constant of something really small like 0.01. Then, create a method called accelerate and put in code to the effect of:
pos = CGPointMake(pos.x + ACC_CONSTANT, pos.y + ACC_CONSTANT);
pos2 = CGPointMake(pos2.x + ACC_CONSTANT, pos.y + ACC_CONSTANT);
Call accelerate after [self checkCollision];
Then play around with the Acceleration constant until you get the results you want.
If you want it to be a step function where it only increases every second, you will need to use an NSTimer iVar, which would also be fairly easy. Just instantiate it with a one second time interval using this init function
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
Then call #selector(accelerate) as the target selector.

Display an image for 1 sec on the view

What I want to realize is:
When I touch a button, the image display on the view for 1 sec, then the image disappear.
I know that a NSTimer will help, but i dont know how to write the right code...need your help, thanks.
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showPictures:) userInfo:nil repeats:NO];
}
- (void)showPictures:(NSTimer *)timer {
bodytouchedimage.hidden = YES;
}
What you should to is call the showPictures function when you touch the button and then within the showPictures method you add a NSTimer that will call a hidePictures method 1 second later
- (void)showPictures
{
bodytouchedimage.hidden = NO;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hidePictures) userInfo:nil repeats:NO];
}
- (void)hidePictures
{
bodytouchedimage.hidden = YES;
}
Rather than using NSTimer, it would be easier to simply call your method to hide the image like this:
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
[self performSelector:#selector(hidePicture) withObject:nil afterDelay:1];
}
- (void)hidePicture {
bodytouchedimage.hidden = YES;
}
performSelector:withObject:afterDelay: is a method of the NSObject class.

location different CGPoint

Hi
how do I locate both the cgpoint? he gives me just one.
-(void)gestureLoad {
//GESTURE
UIGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(numTap2:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
}
- (void)numTap2:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];
...other actions...
}
thanks a lot!
From the description of -[UIGestureRecognizer numberOfTouches]:
Using the value returned by this
method in a loop, you can ask for the
location of individual touches using
the locationOfTouch:inView: method.
So, call -locationOfTouche:inView: for each touch to get the corresponding location.