I have birds flying within a frame in my game, but I can only get them to fly in two different directions. If there are 2 birds they go in two different directions. If there are 3 birds, 2 of them go in one direction and the other one goes in a different direction. I want the birds to randomly go in four different directions. Up right, down right, up left and down left, here is my code.
-(void) AddBirdIntoArray: (int) BirdCount {
for(int i=0; i< BirdCount ; i++){
if(appDelegate.enemyselect == 0){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImage];
[imgBird[i] setAnimationImages:birdArrayConstant];
}
else if(appDelegate.enemyselect == 1){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImagegreenorange];
[imgBird[i] setAnimationImages:birdArrayConstant3];
}
else if(appDelegate.enemyselect == 2){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImageblueyellow];
[imgBird[i] setAnimationImages:birdArrayConstant4];
}
else if(appDelegate.enemyselect == 3){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImagebluewhite];
[imgBird[i] setAnimationImages:birdArrayConstant2];
}
else if(appDelegate.enemyselect == 4){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImagepinkpurple];
[imgBird[i] setAnimationImages:birdArrayConstant5];
}
else if(appDelegate.enemyselect == 5){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImagebluegreen];
[imgBird[i] setAnimationImages:birdArrayConstant6];
}
else if(appDelegate.enemyselect == 6){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImageorangewhite];
[imgBird[i] setAnimationImages:birdArrayConstant7];
}
else if(appDelegate.enemyselect == 7){
imgBird[i]=[[UIImageView alloc] initWithImage:firstImageredblue];
[imgBird[i] setAnimationImages:birdArrayConstant8];
}
[imgBird[i] setAnimationDuration:1.0];
[imgBird[i] startAnimating];
if(i%2==0){
pos[i]=CGPointMake(-1,1);
}
else{
pos[i]=CGPointMake(1,-1);
}
xvalue = arc4random()%250;
yvalue = arc4random()%250;
CGRect TempRect = CGRectMake(xvalue ,yvalue , 22 , 22);
imgBird[i].frame = TempRect;
[birdImageViewArray addObject:imgBird[i]];
[self addSubview:imgBird[i]];
[imgBird[i] release];
}
[birdArray release];
}
I think these are your two direction vectors:
pos[i]=CGPointMake(-1,1);
pos[i]=CGPointMake(1,-1);
The other two directions are:
pos[i]=CGPointMake(-1,-1);
pos[i]=CGPointMake(1,1);
And, of course, rather than the if/else based on i%2, you should use a switch based on:
arc4random()%4
Related
I would like to know how to correctly operate with two views by UISegmentController.
Now I have two UIViews and UISegmentController and procedure changeView:
- (void)changeView:(NSInteger)index {
switch (index) {
case 0:
self.recipeInfoView.alpha = 1;
self.recipeInfoView2.alpha = 0;
break;
case 1:
self.recipeInfoView.alpha = 0;
self.recipeInfoView2.alpha = 1;
break;
default:
break;
}
This code works, but each view have the same position and size and very uncomfortable to work with it.
I'm using storyboards.
First write following code For create UISegmentedControl and give color of your UIView.
- (void)viewDidLoad
{
[super viewDidLoad];
//Make hide of your UIView
self.recipeInfoView.hide = YES;
self.recipeInfoView2.hide = YES;
//Give color of your UIView
self.recipeInfoView.backgroundColor = [UIColor redColor];
self.recipeInfoView.backgroundColor = [UIColor blackColor];
//Create UISegmentedControl Controller
NSArray *itemArray = [NSArray arrayWithObjects: #"FirstView", #"SecondView", nil];
self.segmentedControl= [[UISegmentedControl alloc] initWithItems:itemArray];
self.segmentedControl.frame = CGRectMake(35, 100, 250, 33);
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.segmentedControl.momentary=NO;
self.segmentedControl.tintColor=[UIColor darkGrayColor];
[self.segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.segmentedControl];
}
// segmentAction Methods
- (IBAction)segmentAction:(id)sender
{
if([sender selectedSegmentIndex] == 0)
{
if(self.recipeInfoView.isHidden == YES)
self.recipeInfoView.Hidden == NO;
else
self.recipeInfoView.Hidden == NO;
if(self.recipeInfoView2.isHidden == YES)
self.recipeInfoView2.Hidden == YES;
else
self.recipeInfoView2.Hidden == YES;
}
else if([sender selectedSegmentIndex] == 1)
{
if(self.recipeInfoView2.isHidden == YES)
self.recipeInfoView2.Hidden == NO;
else
self.recipeInfoView2.Hidden == NO;
if(self.recipeInfoView.isHidden == YES)
self.recipeInfoView.Hidden == YES;
else
self.recipeInfoView.Hidden == YES;
}
}
set view property hidden = YES or NO instead of setting alpha for view
good day.. I have this code.. and this two is for actionAnimating Button and stopAnimating Button..I just want to know if how to make those two button integrate in ONE button.. for example.. I click the button then it animates.. when I click again that actionAnimating Button, i want to cancel/stop the animation at the same time i want to play/animate again.. in short i want to cancel the animation when i click again the actionAnimating button..
-(void)flowerAnimationSequence//START ANIMATION
{
MotherView.alpha = 0;
flower.alpha = 0;
[animationContainer1 removeFromSuperview];
actselected = YES;
NSLog(#"start");
if (((sequenceAnimateCounter == 0) || (sequenceAnimateCounter==1)) && (actselected = YES))
{
aImageViewSlideShow = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
aImageViewSlideShow.tag = 171;
[self.view addSubview:aImageViewSlideShow];
}
if (sequenceAnimateCounter < 183)
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.035 target:self selector:#selector(flowerAnimationSequence) userInfo:nil repeats:NO];
}
else if (sequenceAnimateCounter ==183)
{
aImageView = (UIImageView *)[self.view viewWithTag:171];
NSLog(#"done");
actselected = NO;
sequenceAnimateCounter = 0;
[aImageView removeFromSuperview];
aImageView = nil;
[self DefaultPosition];
}
aImageView = (UIImageView *)[self.view viewWithTag:171];
NSString *aStrNumber = [NSString stringWithFormat:#"%i",sequenceAnimateCounter];
NSString *aBundlePath = [[NSBundle mainBundle]bundlePath];
NSString *aImagePath = [NSString stringWithFormat:#"%#/sapatos_%#.png",aBundlePath,aStrNumber];
[aImageView setImage:[UIImage imageWithContentsOfFile:aImagePath]];
[self.view bringSubviewToFront: aImageView];
sequenceAnimateCounter = sequenceAnimateCounter+1;
if (sequenceAnimateCounter == 1)
{
[aImageView removeFromSuperview];
}
}
-(void)stopanim//STOP BUTTON
{
[timer invalidate];
sequenceAnimateCounter =0;
NSLog(#"stop");
[aImageView removeFromSuperview];
}
thanks in advance!
Hm...
you want to start/stop in one button?
BOOL isAnimating = NO;
- (void)startStopAnimating
{
if (!isAnimating) {
isAnimating = YES
// start animation
} else {
isAnimating = NO;
// stop animation
}
}
I have UIScrollView in stage and paging functionality enabled to move back/next.
I have added 10 subviews in UIScrollView. When I have modify the content inside the subviews, then it is not reflected in UIScrollView.
Book.m
- (void) initializeWithXML:(NSString *)XMLURLString {
NSData *xmlData;
NSString *url;
if ( ![applicationData getMode] ) {
resourceRootURL = [[applicationData getAssetsPath] stringByAppendingPathComponent:#"phone/"];
} else {
resourceRootURL = [applicationData getAssetsPath];
}
url = [resourceRootURL stringByAppendingPathComponent:XMLURLString];
//NSLog(#"Root URL...%#", url);
if ([url rangeOfString:#"http"].location != NSNotFound) {
xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
} else {
xmlData = [NSData dataWithContentsOfFile:url];
}
bookContentArray = [self grabXML:xmlData andQuery:#"//page"];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
for(int i = 0; i < [bookContentArray count]; i++) {
[viewControllers addObject:[NSNull null]];
}
if ( isTwoPage ) {
kNumberOfPages = [bookContentArray count];
} else {
kNumberOfPages = [bookContentArray count]/2;
}
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
scrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor blackColor];
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = [self contentSizeForPagingScrollView];
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
scrollView.delegate = self;
self.view = scrollView;
kNumberOfPages = [bookContentArray count];
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadPage:0];
}
- (void) loadPage:(int)number {
// Calculate which pages are visible
int firstNeededPageIndex = MAX(number-1, 0);
int lastNeededPageIndex = MIN(number+1, [viewControllers count] - 1);
//NSLog(#"%d,%d",firstNeededPageIndex,lastNeededPageIndex);
// Recycle no-longer-visible pages
for(int i = 0; i < [viewControllers count]; i++) {
ImageScrollView *page = [viewControllers objectAtIndex:i];
if ((NSNull *)page != [NSNull null]) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
//NSLog(#"removed page %d", page.index);
[page removeImages];
[page removeFromSuperview];
page = nil;
[viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
}
}
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
if ( number == 0 ) {
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
} else if ( number == [bookContentArray count]-1) {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
} else {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
}
}
- (void)loadScrollViewWithPage:(int)page {
ImageScrollView *pageController = [[ImageScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
pageController.index = page;
pageController.myDelegate = self;
pageNumber = pageControl.currentPage;
pageController.isTwoPage = isTwoPage;
if ( pageNumber == page ) {
pageController.isCurrentPage = YES;
} else {
pageController.isCurrentPage = NO;
}
[controller setImageURL:leftURL andRightURL:rightURL andPriority:0];
[scrollView addSubview:controller];
}
ImageScrollView.m
- (void) setImageURL:(NSString *)leftURL andRightURL:(NSString *)rightURL andPriority:(int)priority {
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
if ( !isTwoPage ) {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
} else {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
}
if ( isTwoPage ) {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = CGRectMake(0, 43, 512, 682);
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = CGRectMake(512, 43, 512, 682);
} else {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = imageView.frame;
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = imageView.frame;
}
leftImage.delegate = self;
rightImage.delegate = self;
if ( isFirstPage ) {
[leftImage.view setHidden:YES];
[rightImage.view setHidden:NO];
} else if ( isLastPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else if ( !isTwoPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:NO];
}
imageView.backgroundColor = [UIColor whiteColor];
[self addSubview:imageView];
[leftImage setImageURL:leftURL];
[rightImage setImageURL:rightURL];
[imageView addSubview:leftImage.view];
[imageView addSubview:rightImage.view];
}
FBPage.m
- (void) setImageURL:(NSString *)url {
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 768, 1024);
imageView.center = self.view.center;
imageView.userInteractionEnabled = TRUE;
[container addSubview:imageView];
[activityIndicator setHidden:YES];
if ([url rangeOfString:#"http"].location != NSNotFound) {
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:[NSURL URLWithString:url]];
if (cachedImage) {
[imageView setImage:cachedImage];
isImageLoaded = YES;
[self addScrollView];
} else {
[activityIndicator setHidden:NO];
[activityIndicator startAnimating];
[manager downloadWithURL:[NSURL URLWithString:url] delegate:self options:0 success:^(UIImage *image){
[activityIndicator stopAnimating];
[activityIndicator setHidden:YES];
[imageView setImage:image];
isImageLoaded = YES;
[self addScrollView];
} failure:nil];
}
imageURL = url;
} else {
imageView.image = [UIImage imageWithContentsOfFile:url];
isImageLoaded = YES;
[self addScrollView];
}
}
- (void) setHotspotURL:(NSString *)URL {
[hotspot.view removeFromSuperview];
[hotspot release];
hotspot = nil;
hotspot = [[FBHotspot alloc] initWithNibName:#"FBHotspot" bundle:nil];
hotspot.view.frame = CGRectMake(0, 0, 768, 1024);
hotspot.delegate = self;
[hotspot setURL:URL];
[container addSubview:hotspot.view];
}
**When I am calling setHotspotURL and it’s not updating to the scrollview.**
Make sure you inserted your content at the correct position within the scroll view. Set the contentSize accordingly. Try to call layoutSubviews of the UIScrollView instance
I have a UIbutton which i want to work as a joystick. So i am trying to add some gesture recognizer on the same button.
I have this in my code right now:
#implementation CUETutorialSixteenClusterRootController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:#"CUETutorialLandscapeClusterRootController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (id)init {
self = [super init];
if (self) {
_leftInActiveView = [[NSMutableArray alloc] init];
_rightInActiveViews = [[NSMutableArray alloc] init];
_centerInActiveViews = [[NSMutableArray alloc]init];
_centerActiveViews = [[NSMutableArray alloc]init];
UIImageView * inActLeftView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActLeftView.image = [UIImage imageNamed:#"1-am-station_01.png"];
[_leftInActiveView addObject:inActLeftView];
UIImageView * inActRightView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActRightView.image = [UIImage imageNamed:#"1-am-station_03.png"];
[_rightInActiveViews addObject:inActRightView];
NSArray * imagesName = [NSArray arrayWithObjects:[UIImage imageNamed:#"1_start.png"],
[UIImage imageNamed:#"2_apptray_audio_selected.png"],
[UIImage imageNamed:#"2_apptray_phone_selected.png"],
[UIImage imageNamed:#"4_phonemenu_contacts_highlighted.png"],
[UIImage imageNamed:#"5_phonemenu_recentcalls_highlighted.png"],
[UIImage imageNamed:#"6_recent_calls.png"],
[UIImage imageNamed:#"7_bottom_hit.png"],
[UIImage imageNamed:#"8_recent_call_details.png"],
[UIImage imageNamed:#"9_calling.png"],
[UIImage imageNamed:#"9_calling_myphone.png"],
[UIImage imageNamed:#"9_call_ended.png"],nil];
NSLog(#"Count:%d",[imagesName count]);
for (int i =0; i<11; i++) {
UIImageView * inActCenterView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 226, 98)] autorelease];
inActCenterView.image = [imagesName objectAtIndex:i];
[_centerActiveViews addObject:inActCenterView];
[_centerInActiveViews addObject:inActCenterView];
}
}
return self;
}
-(void)showClusterType:(clusterType)cluster{
switch (cluster) {
//Base Cluster is analog
case kClusterTypeBase:
{
[self.fullClusterContainerView removeFromSuperview];
[self.view addSubview:self.baseClusterContainerView];
[self.view addSubview:self.baseClusterImageView];
CUETutorialSixteenAplicationHomeScreen * clusAppScreen = [[CUETutorialSixteenAplicationHomeScreen alloc] initWithDelegate:self];
clusAppScreen.dataSource = self;
clusAppScreen.rootController = self;
self.clusterHomeScreen = clusAppScreen;
[self.baseClusterContainerView addSubview:clusAppScreen.view];
[clusAppScreen release];
/*
[self.view bringSubviewToFront:self.steeringWheelImageView];
[self.view bringSubviewToFront:self.promptLabel];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavDownButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavUpButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonUp]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonLeft]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonRight]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonDown]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonCenter]];
*/
[self.view bringSubviewToFront:self.promptLabel];
}
break;
//Full cluster is digital
case kClusterTypeFull:
{
[self.baseClusterImageView removeFromSuperview];
[self.baseClusterContainerView removeFromSuperview];
[self.view addSubview:self.fullClusterContainerView];
}
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
_step =1;
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
time=0;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)dealloc{
[super dealloc];
[_leftInActiveView release];
[_rightInActiveViews release];
[_centerActiveViews release];
[_centerInActiveViews release];
}
-(void)animateToView:(UIView *)aview{
if(aview){
//CGRect rect = [aview convertRect:aview.bounds toView:self.view];
CGRect rect = aview.frame;
[self animateHandFromRect:self.promptLabel.frame toRect:rect];
}
else{
[self animateHandToNSValueRect:nil];
}
}
-(void)finishTutorial{
[self displayPromptText:#"TUTORIAL COMPLETE"];
[self displayNavigationBarWithTitle:#"TUTORIAL COMPLETE"];
[self performBlock:^{
[self.clusterHomeScreen.view removeFromSuperview];
[self showScoresScreen];
}afterDelay:1];
}
-(void)animateHandToCenterButton
{
[self displayPromptText:#"TAP RIGHT SIDE CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE CENTER BUTTON"];
[self animateToView:self.rCenterButton];
}
-(void)animateHandToDownButton {
[self displayPromptText:#"TAP RIGHT SIDE DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE DOWN BUTTON"];
[self animateToView:self.rDownButton];
}
-(void)animateHandToUpButton{
[self displayPromptText:#"TAP RIGHT SIDE UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE UP BUTTON"];
[self animateToView:self.rUpButton];
}
-(void)replaceCallScreen{
[self performBlock:^{
[self pressedRightButtonCenter];
} afterDelay:2.0];
}
-(void)changeTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(startTicking) userInfo:nil repeats:YES];
}
-(void)startTicking {
time = time +1;
self.clusterHomeScreen.callTimer.text = [NSString stringWithFormat:#"00:0%d",time];
if (time==9) {
[timer invalidate];
}
}
-(void) animateHandToVolUpButton {
[self displayPromptText:#"TAP VOLUME UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME UP BUTTON"];
[self animateToView:self.rVolUpButton];
}
-(void) animateHandToVolDownButton {
[self displayPromptText:#"TAP VOLUME DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME DOWN BUTTON"];
[self animateToView:self.rVolDownButton];
}
-(void)removeVolumeView {
[self pressedRightButtonCenter];
[self.clusterHomeScreen runNextStep];
}
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.%#",recognizer);
}
-(IBAction)buttonClicked:(id)sender
{
if ([sender tag]==kRightButtonDown && _step ==1)
{
[self pressedRightButtonDown];
[self displayPromptText:#"TAP AGAIN"];
[self displayNavigationBarWithTitle:#"TAP AGAIN"];
_step++;
}
else if ([sender tag]==kRightButtonDown && _step == 2)
{
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step == 3)
{
[self pressedRightButtonCenter];
[self animateToView:self.rUpButton];
_step++;
}
else if ([sender tag]==kRightButtonUp && _step == 4)
{
[self pressedRightButtonUp];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
[self animateToView:self.rCenterButton];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==5){
[self pressedRightButtonCenter];
[self displayPromptText:#"TAP DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP DOWN BUTTON"];
[self animateToView:self.rDownButton];
_step++;
}
else if([sender tag]==kRightButtonDown && _step==6){
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==7){
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==8)
{
[self pressedRightButtonCenter];
self.clusterHomeScreen.callTimer.hidden = NO;
[self.clusterHomeScreen runNextStep];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==9)
{
self.clusterHomeScreen.callTimer.hidden = YES;
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==10)
{
[self pressedRightButtonCenter];
[self finishTutorial];
}
else {
[self displayVisualFeedback:kVisualFeedBackIncorrectAnswer];
}
}
#pragma CUEClusterHomeSreenDataSource Methods
-(NSInteger)numberOfViewsForLeftScreen {
return [_leftActiveViews count];
}
-(NSInteger)numberOfViewsForRightScreen{
return [_rightActiveViews count];
}
-(NSInteger)numberOfViewsForCenterScreen {
return [_centerActiveViews count];
}
-(UIImageView *)inActiveViewForLeftScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_leftInActiveView objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForLeftScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_leftActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightInActiveViews count]) && (index >=0)){
return [_rightInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightActiveViews count]) && (index >=0)){
return [_rightActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForCenterScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_centerInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForCenterScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_centerActiveViews objectAtIndex:index];
//}
//return nil;
}
This is the error i am getting now:
-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0
2012-03-28 13:25:55.724 CUETrainer[1788:11f03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0'
But its not actually doing anything.
Please help!!!!
The error says unrecognized selector sent to instance 0x79b71b0' most probably the names in .h and .m are different..
The target of the gesture recognizer needs to be the object that implements the selector. What's happening now when a swipe happens is that the gesture recognizer is doing [self.gestureReceiverButton handleSwipeFrom:recognizer]
You probably want this:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
Change every instance of:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
to
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
Problem solved. Be very careful with your #selectors, what you place in this macro is NOT validated by the compiler. Code complete will help you with this.
Also you are adding four gesture recognizers to one view, while you can accomplish the same task with one. You can combine the valid directions using the bitwise OR operator, like so:
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown)];
I'm using a scrollview and a pagecontrol to render several images, very similar to the built-in photo app. I'm using this code to setup the controllers when the view loads:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [photos count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
scrollArea.pagingEnabled = YES;
scrollArea.contentSize = CGSizeMake(newW * [photos count], newH);
scrollArea.showsHorizontalScrollIndicator = NO;
scrollArea.showsVerticalScrollIndicator = NO;
scrollArea.scrollsToTop = NO;
scrollArea.delegate = self;
pageControl.numberOfPages = [photos count];
newW and newH are just two variables I'm using to keep track of the size when the orientation changes.
My loadScrollViewWithPage method:
- (void) loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [photos count]) return;
PhotoViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[PhotoViewController alloc] init];
controller.imgPath = (NSString *)[photos objectAtIndex:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview) {
CGRect frame = scrollArea.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollArea addSubview:controller.view];
}
}
And here is my issue. I need to unload controllers/views when I need it. I thought I could use something like this:
for (unsigned i = 0; i < [photos count]; i++) {
[self.viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
I want to do it, so load completely new views into the scrollview.
I also tried something liek this with no luck:
for(UIView *subview in [self.scrollArea subviews]) {
[subview removeFromSuperview];
}
Any ideas?
So you want to make sure the subviews of scrollView stay in sync with the items in self.viewControllers as you update the latter? The easiest thing is to do something like this whenever you replace one of the items:
UIViewController *vc = [self.viewControllers objectAtIndex:i];
if ((NSNull *)vc != [NSNull null] && [vc isViewLoaded]) [vc.view removeFromSuperview];
[self.viewControllers replaceObjectAtIndex:i withObject:foo];