I'm working on a project where images are spawned and you basically touch them and they are removed. How would I removed the object that I touched? I've thought of making a mutablearray to hold all the objects but i can't seem to figure anything out.
GameViewController.m
#import "GameViewController.h"
#import "Cig.h"
#interface GameViewController ()
#end
#implementation GameViewController
#synthesize scoreLbl, timeLbl;
//CLASS ONLY VARS
BOOL isGameOver;
int timeInt;
int scoreInt;
int cigsOnScreen;
NSMutableArray *spawnedCigs;
//CLASS ONLY VARS
//TIMER
-(void)count {
timeInt--;
timeLbl.text = [NSString stringWithFormat:#"Time: %i", timeInt];
if(timeInt == 0){
isGameOver = YES;
NSLog(#"Your Score For This Round: %i", scoreInt);
}
if(isGameOver == NO){
[self performSelector:#selector(count) withObject:nil afterDelay:1];
}
}
//TIMER
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)spawnCigs {
for(int i =0 ; i < 5; i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(arc4random()%760, arc4random()%430, 100, 23)];
UIImage *image = [UIImage imageNamed:#"Cig.png"];
[imageView setImage:image];
Cig *cig = [[Cig alloc] init];
[cig setTag:arc4random()%666];
[cig setImage:imageView];
[spawnedCigs addObject:cig];
[self.view addSubview:imageView];
}
[self performSelector:#selector(spawnCigs) withObject:nil afterDelay:5];
}
-(void)reset {
scoreLbl.text = #"Score:";
timeLbl.text = #"Time:";
isGameOver = NO;
timeInt = 60;
scoreInt = 0;
cigsOnScreen = 0;
spawnedCigs = [NSMutableArray arrayWithObjects:nil, nil];
[self performSelector:#selector(count) withObject:nil afterDelay:1];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self reset];
[self spawnCigs];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Code is pretty messy so please don't judge me on that.
Thanks for any help that is provided
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[[event allTouches]anyObject];
CGPoint touchPoint = [touch locationInView:touch.view];;
for (UIView *view in [self.view subviews])
{
if([view isMemberOfClass:[UIImageView class]])
{
if (CGRectContainsPoint(view.frame,touchPoint))
{
[view removeFromSuperview];
}
}
}
}
this helps you
use
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIImageView *view in [self.view subviews])
{
if (view.tag==1)
{
[view removeFromSuperview];
}
if (view.tag==2)
{
[view removeFromSuperview];
}
}
}
try this your issue will be resolved. Don't forget to pass tab to your imageviews...
By using
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method you will get the image view of selected image. simply remove the object from superview.
Related
I'm trying to update my app to use a storyboard with auto-layout. Using a XIB, I have this working fine. My app has a view controller with a subView defined in Interface Builder. I draw in the subView.
That works.
But I cannot clear (or undo/redo). I can see that the UIView methods are invoked, but I must not be doing something I'm supposed to do.
Here are some code snips, and I'd appreciate some help:
VC simply calls the view:
- (IBAction)eraseButtonTapped:(id)sender {
NSLog(#"%s", __FUNCTION__);
savedImage.image = drawImage.image;
[drawingView eraseButtonClicked];
}
UIView:
- (void)eraseButtonClicked {
NSLog(#"%s", __FUNCTION__);
self.bufferArray = [self.currentArray mutableCopy];
[self.currentArray removeAllObjects];
[self setNeedsDisplay];
}
Both functions log..
These are the touch methods:
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"%s", __FUNCTION__);
self.currentColorPath = [[DrawingPath alloc] init];
[self.currentColorPath setColor:self.currentColor];
UITouch *touch= [touches anyObject];
[self.currentColorPath.path moveToPoint:[touch locationInView:self]];
[self.currentArray addObject:self.currentColorPath];
[self.redoStack removeAllObjects];
lastPoint = [touch locationInView:self];
lastPoint.y -= 20;
if ([touch tapCount] == 2) {
[self alertOKCancelAction];
return;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"%s", __FUNCTION__);
UITouch *touch = [touches anyObject];
[self.currentColorPath.path addLineToPoint:[touch locationInView:self]];
[self setNeedsDisplay];
CGPoint currentPoint = [touch locationInView:self];
currentPoint.y -= 20;
lastPoint = currentPoint;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"%s", __FUNCTION__);
self.currentColorPath = nil;
}
The view calls a separate object to handle the drawing.. This is a code snippet (removing non-relevant bits).
- (id)init {
//NSLog(#"%s", __FUNCTION__);
if (!(self = [super init] ))
return nil;
path = [[UIBezierPath alloc] init];
_path.lineCapStyle=kCGLineCapRound;
_path.lineJoinStyle=kCGLineJoinRound;
[_path setLineWidth:brush];
return self;
}
- (void)draw {
NSLog(#"%s", __FUNCTION__);
[self.color setStroke];
[self.path stroke];
}
I am having some physics body nodes. Sprite Kit immediately calls didbegincontact method. I want that method should be called when i release the touch so that it may perform actions when click was released instead of calling method immediately causing some action setting problems for me.
- (void)didBeginContact:(SKPhysicsContact *)contact
{ NSLog(#"%hhd", _touching);
if(_touching == NO)
return;
something here
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_touching = YES;
NSLog(#"%hhd", _touching);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_touching = NO;
NSLog(#"%hhd", _touching);
something here
}
Set the global Variable, ie
BOOL _touching;
When you touch / release (touches ended and began) you set that var to YES / NO;
In the didbegincontact, use something like
if(_touching == YES) {
// what I want to happen when I am touching
}
else {
// i must not be touching so do this
}
Here is the basic set up - however I think its the game logic thats the issue, maybe think of a different way to solve your problem
#interface XBLMyScene()
#property (strong, nonatomic) SKNode *world;
#property (strong, nonatomic) SKSpriteNode *ball;
#property BOOL touching;
#end
#implementation XBLMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.world = [SKNode node];
[self addChild:self.world];
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(500, 0)];
self.ball = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(40, 40)];
self.ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 40)];
self.ball.position = CGPointMake(200, 300);
[self.world addChild:self.ball];
self.touching = NO;
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.touching = YES;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.touching = NO;
}
- (void) didSimulatePhysics
{
if (self.touching) {
NSLog(#"I am touching");
}
else {
NSLog(#"I am not touching");
}
}
Problem:
I have a UIView that has a UITableView as a subview. The UIView has a UITapGestureRecognizer configured.
My Problem is that taps on the table are consumed by the UIView's gesture recognizer. The result is the table view never gets to see taps.
How can I either make the recognizer fail when a point is within the table's frame OR make the table the default consumer of the tap.
I have tried (as the code examples show) a number of methods pointInside:withEvent, hitTest:withEvent but can't quite figure out how to do it.
Here is code representing the problem:
Controller:
#import <UIKit/UIKit.h>
#import "ABCDTableView.h"
#interface ABCDFirstView : UIView
#property (nonatomic,strong) ABCDTableView *tableView;
#end
#import "ABCDFirstView.h"
#implementation ABCDFirstView
#synthesize tableView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(viewTouch:)];
[self addGestureRecognizer:tap];
}
- (void)viewTouch:(UIGestureRecognizer *)gesture {
NSLog(#"view touched");
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint(self.tableView.bounds, point)) {
NSLog(#"point in table point as well as view");
return NO;
}
else if (CGRectContainsPoint(self.bounds, point)) {
NSLog(#"point only in view");
return YES;
}
NSLog(#"point not in view");
return NO;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
NSLog(#"view hittest res: %#",view);
return view;
}
#end
TableView
#import <UIKit/UIKit.h>
#interface ABCDTableView : UITableView
<UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSArray *list;
#end
#import "ABCDTableView.h"
#implementation ABCDTableView
#synthesize list;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
self.delegate = self;
self.dataSource = self;
// create table list
self.list = [[NSArray alloc]
initWithObjects: #"one",#"two",#"three",#"four",#"five",
#"six", #"seven", #"eight", #"nine", #"ten", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"cell";
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [self.list objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"row selected");
}
#end
View
#import <UIKit/UIKit.h>
#import "ABCDTableView.h"
#interface ABCDFirstView : UIView
#property (nonatomic,strong) ABCDTableView *tableView;
#end
#import "ABCDFirstView.h"
#implementation ABCDFirstView
#synthesize tableView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(viewTouch:)];
[self addGestureRecognizer:tap];
}
- (void)viewTouch:(UIGestureRecognizer *)gesture {
NSLog(#"view touched");
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint(self.tableView.bounds, point)) {
NSLog(#"point in table point as well as view");
return NO;
}
else if (CGRectContainsPoint(self.bounds, point)) {
NSLog(#"point only in view");
return YES;
}
NSLog(#"point not in view");
return NO;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
NSLog(#"view hittest res: %#",view);
return view;
}
#end
Use the UIGestureRecognizerDelegate method
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
and check if the touch.view is the same view as the one that should receive the gesture recognizer.
I am working on a magazine viewer and I have to use UIWebView for pages because of html5 interactive contents. First I tried uiwebviews in a UIScrollView but scrollview was too slow on sliding pages.
So now I am trying to write my own scrollview-like code. I have a MainView.xib, a view controller (Viewer) and extended UIWindow class (TouchCapturingWindow) that I take from here : http://wyldco.com/blog/2010/11/how-to-capture-touches-over-a-uiwebview/
when I try to slide pages fast, there is no problem but when I touch and slowly slide by not pulling my finger, "Viewer" view controller stops receiving touch events and so pages stop moving. I am logging TouchCapturingWindow, it still sending events. I searched many information and tutorials but I couldn't make it work. How can I make it continuously receive touch events?
I uploaded a simple Xcode project that contains only this part of my project. You can download here : http://testdergi.mysys.com/touchEvents.zip
When you run project, you should first download pages (180Kb) by tapping "Download Pages" button, then tap the "Open Viewer" button.
You can also look over the code below :
TouchCapturingWindow.h :
#interface TouchCapturingWindow : UIWindow {
NSMutableArray *views;
#private
UIView *touchView;
}
- (void)addViewForTouchPriority:(UIView*)view;
- (void)removeViewForTouchPriority:(UIView*)view;
#end
TouchCapturingWindow.m :
#implementation TouchCapturingWindow
- (void)dealloc {
}
- (void)addViewForTouchPriority:(UIView*)view {
if ( !views ) views = [[NSMutableArray alloc] init];
[views addObject:view];
}
- (void)removeViewForTouchPriority:(UIView*)view {
if ( !views ) return;
[views removeObject:view];
}
- (void)sendEvent:(UIEvent *)event {
//get a touch
UITouch *touch = [[event allTouches] anyObject];
//check which phase the touch is at, and process it
if (touch.phase == UITouchPhaseBegan) {
for ( UIView *view in views ) {
//if ( CGRectContainsPoint([view frame], [touch locationInView:[view superview]]) ) {
NSLog(#"TouchCapturingWindow --> TouchPhaseBegan");
touchView = view;
[touchView touchesBegan:[event allTouches] withEvent:event];
break;
}
}
else if (touch.phase == UITouchPhaseMoved) {
NSLog(#"TouchCapturingWindow --> TouchPhaseMoved");
if ( touchView ) {
[touchView touchesMoved:[event allTouches] withEvent:event];
}
else
{
NSLog(#"touch view is nil");
}
}
else if (touch.phase == UITouchPhaseCancelled) {
NSLog(#"TouchCapturingWindow --> TouchPhaseCancelled");
if ( touchView ) {
[touchView touchesCancelled:[event allTouches] withEvent:event];
touchView = nil;
}
}
else if (touch.phase == UITouchPhaseEnded) {
NSLog(#"TouchCapturingWindow --> TouchPhaseEnded");
if ( touchView ) {
[touchView touchesEnded:[event allTouches] withEvent:event];
touchView = nil;
}
}
//we need to send the message to the super for the
//text overlay to work (holding touch to show copy/paste)
[super sendEvent:event];
}
#end
Viewer.h :
#interface Viewer : UIViewController{
int currentPage;
int totalPages;
IBOutlet UIView *pagesView;
int lastTchX;
int difference;
BOOL hasMoved;
int touchBeganSeritX;
}
- (IBAction)backBtnClicked:(id)sender;
- (void)loadImages;
- (void)animationDidStop;
- (void)loadSinglePage:(int)pageNo;
#property (nonatomic, retain) IBOutlet UIView *pagesView;
#end
Viewer.m :
#interface Viewer ()
#end
#implementation Viewer
#synthesize pagesView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)backBtnClicked:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)loadImages
{
for(UIView *subView in pagesView.subviews)
{
[subView removeFromSuperview];
}
currentPage = 1;
totalPages = 12;
for(int count = 1; count <= 12; count++)
{
[self loadSinglePage:count];
}
}
- (void)loadSinglePage:(int)pageNo
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory , NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
NSString *pagesDir = [NSString stringWithFormat:#"%#/pages", cachesDir];
int pageX = (pageNo - 1) * 768;
UIWebView *aPageWebView = [[UIWebView alloc] init];
[aPageWebView setFrame:CGRectMake(pageX, 0, 768, 1024)];
aPageWebView.backgroundColor = [UIColor clearColor];
aPageWebView.opaque = YES;
[aPageWebView setClearsContextBeforeDrawing:YES];
aPageWebView.clipsToBounds = NO;
[aPageWebView setScalesPageToFit:YES];
NSString *hamData = [NSString stringWithFormat:#"<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"user-scalable=yes, width=1024, height=1365, maximum-scale=1.0\"><style type=\"text/css\">body {margin:0; padding:0;}</style></head><body bgcolor=\"#508CCF\"><div id=\"touchable\" style=\"top:0px; left:0px; width:1024px; height:1365px; background-image:url(%#.jpg)\"></div></body></html>", [[NSNumber numberWithInt:pageNo] stringValue]];
[aPageWebView loadHTMLString:hamData baseURL:[NSURL fileURLWithPath:pagesDir isDirectory:YES]];
aPageWebView.scrollView.bounces = NO;
[aPageWebView.scrollView setMaximumZoomScale:1.3333f];
[aPageWebView.scrollView setMinimumZoomScale:1.0f];
aPageWebView.scrollView.zoomScale = 1.0f;
[aPageWebView setMultipleTouchEnabled:YES];
[pagesView addSubview:aPageWebView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
UITouch *myTouch = [[event allTouches] anyObject];
int curTchX = [myTouch locationInView:self.view].x;
lastTchX = curTchX;
hasMoved = NO;
touchBeganSeritX = pagesView.frame.origin.x;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Viewer : .....moved");
hasMoved = YES;
UITouch *myTouch = [[event allTouches] anyObject];
int curTchX = [myTouch locationInView:self.view].x;
difference = curTchX - lastTchX;
int newX = (pagesView.frame.origin.x + difference);
if(newX <= 0)
{
[pagesView setFrame:CGRectMake((pagesView.frame.origin.x + difference), 0, pagesView.frame.size.width, 1024)];
}
lastTchX = curTchX;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
if(hasMoved == YES)
{
hasMoved = NO;
int curSeritX = pagesView.frame.origin.x;
curSeritX = curSeritX / (-1);
int newX = 0;
if(difference < 0) //Sağa geçilecek
{
if((currentPage + 1) <= totalPages)
{
currentPage++;
}
}
else //Sola geçilecek
{
if((currentPage - 1) >= 1)
{
currentPage--;
}
}
newX = (currentPage - 1)*768*(-1);
[UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
// Do your animations here.
[pagesView setFrame:CGRectMake(newX, 0, pagesView.frame.size.width, pagesView.frame.size.height)];
}
completion:^(BOOL finished){
if (finished) {
// Do your method here after your animation.
}
}];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesCancelled");
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
I am answering my own question. I found something after a hard work. I changed "(void)sendEvent:(UIEvent *)event" method like the code below. I saw that use of "[super sendEvent:event];" after UITouchPhaseMoved phase is problematic.
- (void)sendEvent:(UIEvent *)event {
//we need to send the message to the super for the
//text overlay to work (holding touch to show copy/paste)
//[super sendEvent:event];
//get a touch
UITouch *touch = [[event allTouches] anyObject];
//check which phase the touch is at, and process it
if (touch.phase == UITouchPhaseBegan) {
for ( UIView *view in views ) {
//if ( CGRectContainsPoint([view frame], [touch locationInView:[view superview]]) ) {
NSLog(#"TouchCapturingWindow --> TouchPhaseBegan");
touchView = view;
[touchView touchesBegan:[event allTouches] withEvent:event];
break;
}
[super sendEvent:event];
}
else if (touch.phase == UITouchPhaseMoved) {
NSLog(#"TouchCapturingWindow --> TouchPhaseMoved");
if ( touchView ) {
[touchView touchesMoved:[event allTouches] withEvent:event];
int curTchX = [touch locationInView:self].x;
NSLog(#"curTchX : %d", curTchX);
}
else
{
NSLog(#"touch view is nil");
}
}
else if (touch.phase == UITouchPhaseCancelled) {
NSLog(#"TouchCapturingWindow --> TouchPhaseCancelled");
if ( touchView ) {
[touchView touchesCancelled:[event allTouches] withEvent:event];
touchView = nil;
}
[super sendEvent:event];
}
else if (touch.phase == UITouchPhaseEnded) {
NSLog(#"TouchCapturingWindow --> TouchPhaseEnded");
if ( touchView ) {
[touchView touchesEnded:[event allTouches] withEvent:event];
touchView = nil;
}
[super sendEvent:event];
}
//we need to send the message to the super for the
//text overlay to work (holding touch to show copy/paste)
}
But the new problem is; if I add a video in a web page, and try to forward the video with its slider, page is moving. I have to find a way to detect if user taps on a video.
Am getting an "Expected expression" error but can't figure out why on an IBAction method. Have commented out the error.
Can you tell me what's wrong? Thank you.
#import "RTViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface RTViewController () {
AVAudioPlayer *backgroundAudioPlayer;
SystemSoundID burnRubberSoundID;
BOOL touchInCar;
}
#end
#implementation RTViewController
#synthesize car;
#synthesize testDriveButton;
#synthesize backgroundImage;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Road Trip";
NSURL* backgroundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"CarRunning"
ofType:#"aif"]];
backgroundAudioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundURL error:nil];
backgroundAudioPlayer.numberOfLoops = -1;
[backgroundAudioPlayer prepareToPlay];
NSURL* burnRubberURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"BurnRubber" ofType:#"aif"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)burnRubberURL, &burnRubberSoundID);
[testDriveButton setBackgroundImage:[UIImage animatedImageNamed:#"Button" duration:1.0 ] forState:UIControlStateNormal];
}
- (void)viewDidUnload
{
[self setCar:nil];
[self setTestDriveButton:nil];
[self setBackgroundImage:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)TestDrive:(id)sender {
AudioServicesPlaySystemSound(burnRubberSoundID);
[self performSelector:#selector(playCarSound) withObject:self afterDelay:.2];
CGPoint center = CGPointMake(car.center.x, self.view.frame.origin.y + car.frame.size.height/2);
[UIView animateWithDuration:3 animations:^ {
car.center = center;
}
completion:^(BOOL finished) {
[self rotate];
}];
}
-(void)playCarSound {
[backgroundAudioPlayer play];
}
- (void)rotate {
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
void (^animation) () = ^() {
car.transform = transform;
};
void (^completion) (BOOL) = ^ (BOOL finished) {
[self returnCar];
};
[UIView animateWithDuration:3 animations:animation completion:completion];
}
- (void)returnCar {
CGPoint center = CGPointMake(car.center.x, self.view.frame.origin.y + self.view.frame.size.height - car.frame.size.height/2);
void (^animation)() = ^() {
car.center = center;
};
void (^completion)(BOOL) = ^(BOOL finished) {
[self continueRotation];
};
[UIView animateWithDuration:3 animations:animation completion:completion];
}
- (void)continueRotation {
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
void (^animation)() = ^() {
car.transform = transform;
};
void (^completion)(BOOL) = ^(BOOL finished) {
[backgroundAudioPlayer stop];
[backgroundAudioPlayer prepareToPlay];
};
[UIView animateWithDuration:3 animations:animation completion:completion];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if(CGRectContainsPoint(car.frame, [touch locationInView:self.view]))
touchInCar=YES;
else {
touchInCar=NO;
[super touchesBegan:touches withEvent:event];
}
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeGesture];
- (IBAction)handleSwipeGesture:(id)sender { // Expected expression
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Content"];
[[self navigationController]pushViewController:viewController animated:YES];
}
}
#end
The problem is that you never closed out the method
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
with its own curly brace. One visual cue is that the method
- (IBAction)handleSwipeGesture:(id)sender
(where your error is) starts out indented.