zoukankan      html  css  js  c++  java
  • ios 点餐系统

    这个程序的主要界面就是一个TabBarController。总共三个标签,第一个是所有的可点的菜,第二个是已点的菜,第三个是可以留言或者查看所有留言。

    下面是第一个页面:

    右上角的i按钮是添加新菜,每个cell中的order就是点餐咯,可以重复按多次。

    首先说下这个列表的数据是存放在coredata中的,这个项目的coredata有两个实体,一个是dishes保存每一道菜的名字,id,价格,描述,菜系等。还有一个实体是type保存菜系。

    这个项目用coredata的方式,正好是我想学的。

    就是在新建项目的时候勾选use coredata,那么在appdelegate中就会出现与coredata响应的代码了,只需要根据自己的需要稍微修改即可。或者是自己在已有的项目中添加一个coredata文件,然后将一个已经勾选了use coredata的项目中的appdelagate中的一些代码复制过来即可。

    那么在各个文件中我需要用到coredata的时候 要怎么操作呢?看下面的代码:

    1.     HotelAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  
    2.     //connection  
    3.     NSManagedObjectContext *context = [appDelegate managedObjectContext];  
    4.     //table   
    5.     NSEntityDescription *entityDescr =[NSEntityDescription entityForName:@"Dishes" inManagedObjectContext:context];  
    6.     //query  
    7.     NSFetchRequest *request = [[NSFetchRequest alloc] init];  
    8.     [request setEntity:entityDescr];  
    9.     //pred  
    10.     //NSPredicate *pred = [NSPredicate predicateWithString:@"id=1"];  
    11.       
    12.     NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"typeid" ascending:YES];  
    13.     NSArray *arraySortDescriptor = [[NSArray alloc] initWithObjects:nameDescriptor, nil];  
    14.     [request setSortDescriptors:arraySortDescriptor];  
    15.     [arraySortDescriptor release];  
    16.       
    17.     NSError *error;  
    18. //  NSManagedObject *aDish = nil;  
    19. //  NSNumber *id = [NSNumber numberWithInt:1];  
    20.     NSArray *objects = [context executeFetchRequest:request error:&error];  
    21.       
    22.     if(objects != nil){  
    23.         self.list = objects;  
    24.     }     
    	HotelAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    	//connection
    	NSManagedObjectContext *context = [appDelegate managedObjectContext];
    	//table 
    	NSEntityDescription *entityDescr =[NSEntityDescription entityForName:@"Dishes" inManagedObjectContext:context];
    	//query
    	NSFetchRequest *request = [[NSFetchRequest alloc] init];
    	[request setEntity:entityDescr];
    	//pred
    	//NSPredicate *pred = [NSPredicate predicateWithString:@"id=1"];
    	
    	NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"typeid" ascending:YES];
    	NSArray *arraySortDescriptor = [[NSArray alloc] initWithObjects:nameDescriptor, nil];
    	[request setSortDescriptors:arraySortDescriptor];
    	[arraySortDescriptor release];
    	
    	NSError *error;
    //	NSManagedObject *aDish = nil;
    //	NSNumber *id = [NSNumber numberWithInt:1];
    	NSArray *objects = [context executeFetchRequest:request error:&error];
    	
    	if(objects != nil){
    		self.list = objects;
    	}	


    需要拿已存在于coredata中的文件的时候,就用上面的步骤就可以咯 拿到的数据就在最后的self.list中了

    可以看到上面的tableView是自己绘制的,先看下面代码:

    1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    2.       
    3.     NSUInteger row = [indexPath row];  
    4.     NSManagedObject *theObj = [self.list objectAtIndex:row];  
    5.       
    6.     NSNumber *id = [theObj valueForKey:@"id"];  
    7.     NSInteger typeid = [[theObj valueForKey:@"typeid"] intValue];  
    8.     NSLog(@"tpyeid:%d",typeid);  
    9.       
    10.     static NSString *CellIdentifier = @"Cell";  
    11.       
    12.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    13.     if (cell == nil) {  
    14.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
    15.       
    16.     UIColor *elemBgColor = [UIColor clearColor];  
    17.           
    18.     UIImageView *img = [[UIImageView alloc] init];  
    19.     img.frame = CGRectMake(kX, kY, kRowHeight -10, kRowHeight -10);  
    20.     img.tag = kImage;  
    21.     [cell.contentView addSubview:img];  
    22.     [img release];  
    23.       
    24.     UILabel *nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 0, 100, 25)];  
    25.     nameLbl.font = [UIFont systemFontOfSize:14];  
    26.     nameLbl.tag = kName;  
    27.         nameLbl.backgroundColor = elemBgColor;  
    28.     [cell.contentView addSubview:nameLbl];  
    29.     [nameLbl release];  
    30.       
    31.     UILabel *typeLbl = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 70, 25)];  
    32.     typeLbl.font = [UIFont systemFontOfSize:14];  
    33.     typeLbl.tag = kType;  
    34.         typeLbl.backgroundColor = elemBgColor;  
    35.     [cell.contentView addSubview:typeLbl];  
    36.     [typeLbl release];  
    37.       
    38.     UILabel *priceLbl = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 50, 25)];  
    39.     priceLbl.font =[UIFont systemFontOfSize:14];  
    40.     priceLbl.tag = kPrice;  
    41.         priceLbl.backgroundColor = elemBgColor;  
    42.     [cell.contentView addSubview:priceLbl];  
    43.     [priceLbl release];  
    44.       
    45.     UILabel *introLbl = [[UILabel alloc] initWithFrame:CGRectMake(60, 25, 160, 30)];      
    46.     introLbl.tag = kIntro;  
    47.     introLbl.backgroundColor = elemBgColor;  
    48.     introLbl.numberOfLines = 2;  
    49.     introLbl.textColor = [UIColor grayColor];  
    50.     introLbl.font = [UIFont systemFontOfSize:12];  
    51.     [cell.contentView addSubview:introLbl];  
    52.     [introLbl release];  
    53.       
    54.     UILabel *btnBgLbl = [[UILabel alloc] initWithFrame:CGRectMake(230, 25, 60, 30)];  
    55.         btnBgLbl.backgroundColor = elemBgColor;  
    56.     [cell.contentView addSubview:btnBgLbl];  
    57.     [btnBgLbl release];  
    58.           
    59.     UIButton *orderBtn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];  
    60.     orderBtn.frame = CGRectMake(230, 32, 60, 20);  
    61.     [orderBtn setTitle:@"order" forState:UIControlStateNormal];  
    62.     [orderBtn addTarget:self action:@selector(order:) forControlEvents:UIControlEventTouchUpInside];  
    63.       
    64.     [cell.contentView addSubview:orderBtn];  
    65.           
    66.     }  
    67.     cell.tag = 1 + [indexPath row];  
    68.       
    69.     TableCellView *cellView = [[TableCellView alloc] init];  
    70.     cellView._count = [self.list count];  
    71.     cellView._index = [indexPath row];  
    72.     cell.backgroundView = cellView;  
    73.     [cellView release];  
    74.       
    75.       
    76.     NSString *imageName = [NSString stringWithFormat:@"%d.jpg",[id intValue]];  
    77.     UIImageView *img = (UIImageView *)[cell.contentView viewWithTag:kImage];  
    78.     img.image = [UIImage imageNamed:imageName];  
    79.       
    80.     UILabel *nameLbl = (UILabel *)[cell.contentView viewWithTag:kName];  
    81.     nameLbl.text =  [theObj valueForKey:@"name"];//[list objectAtIndex:[indexPath row]];  
    82.       
    83.     UILabel *typeLbl = (UILabel *)[cell.contentView viewWithTag:kType];  
    84.     typeLbl.text = [theObj valueForKey:@"type"];  
    85.       
    86.     UILabel *priceLbl = (UILabel *)[cell.contentView viewWithTag:kPrice];  
    87.     NSNumber *nPrice = [theObj valueForKey:@"price"];  
    88.     priceLbl.text = [NSString stringWithFormat:@"¥%d",[nPrice intValue]];  
    89.       
    90.     UILabel *introLbl = (UILabel *)[cell.contentView viewWithTag:kIntro];  
    91.     NSString *introText = [theObj valueForKey:@"intro"];  
    92.     if([introText length] > 70){  
    93.         introText = [introText substringToIndex:70];  
    94.         introText = [introText stringByAppendingString:@"..."];  
    95.     }  
    96.     introLbl.text = introText;    
    97.     return cell;  
    98. }  
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	
    	NSUInteger row = [indexPath row];
    	NSManagedObject *theObj = [self.list objectAtIndex:row];
    	
    	NSNumber *id = [theObj valueForKey:@"id"];
    	NSInteger typeid = [[theObj valueForKey:@"typeid"] intValue];
    	NSLog(@"tpyeid:%d",typeid);
    	
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    	
    	UIColor *elemBgColor = [UIColor clearColor];
    		
    	UIImageView *img = [[UIImageView alloc] init];
    	img.frame = CGRectMake(kX, kY, kRowHeight -10, kRowHeight -10);
    	img.tag = kImage;
    	[cell.contentView addSubview:img];
    	[img release];
    	
    	UILabel *nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 0, 100, 25)];
    	nameLbl.font = [UIFont systemFontOfSize:14];
    	nameLbl.tag = kName;
    		nameLbl.backgroundColor = elemBgColor;
    	[cell.contentView addSubview:nameLbl];
    	[nameLbl release];
        
    	UILabel *typeLbl = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 70, 25)];
    	typeLbl.font = [UIFont systemFontOfSize:14];
    	typeLbl.tag = kType;
    		typeLbl.backgroundColor = elemBgColor;
    	[cell.contentView addSubview:typeLbl];
    	[typeLbl release];
    	
    	UILabel *priceLbl = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 50, 25)];
    	priceLbl.font =[UIFont systemFontOfSize:14];
    	priceLbl.tag = kPrice;
    		priceLbl.backgroundColor = elemBgColor;
    	[cell.contentView addSubview:priceLbl];
    	[priceLbl release];
    	
    	UILabel *introLbl = [[UILabel alloc] initWithFrame:CGRectMake(60, 25, 160, 30)];	
    	introLbl.tag = kIntro;
    	introLbl.backgroundColor = elemBgColor;
    	introLbl.numberOfLines = 2;
    	introLbl.textColor = [UIColor grayColor];
    	introLbl.font = [UIFont systemFontOfSize:12];
    	[cell.contentView addSubview:introLbl];
    	[introLbl release];
    	
    	UILabel *btnBgLbl = [[UILabel alloc] initWithFrame:CGRectMake(230, 25, 60, 30)];
    		btnBgLbl.backgroundColor = elemBgColor;
    	[cell.contentView addSubview:btnBgLbl];
    	[btnBgLbl release];
    		
    	UIButton *orderBtn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    	orderBtn.frame = CGRectMake(230, 32, 60, 20);
    	[orderBtn setTitle:@"order" forState:UIControlStateNormal];
    	[orderBtn addTarget:self action:@selector(order:) forControlEvents:UIControlEventTouchUpInside];
    	
    	[cell.contentView addSubview:orderBtn];
    		
        }
    	cell.tag = 1 + [indexPath row];
    	
    	TableCellView *cellView = [[TableCellView alloc] init];
    	cellView._count = [self.list count];
    	cellView._index = [indexPath row];
    	cell.backgroundView = cellView;
    	[cellView release];
    	
    	
    	NSString *imageName = [NSString stringWithFormat:@"%d.jpg",[id intValue]];
    	UIImageView *img = (UIImageView *)[cell.contentView viewWithTag:kImage];
    	img.image = [UIImage imageNamed:imageName];
    	
    	UILabel *nameLbl = (UILabel *)[cell.contentView viewWithTag:kName];
    	nameLbl.text =  [theObj valueForKey:@"name"];//[list objectAtIndex:[indexPath row]];
    	
    	UILabel *typeLbl = (UILabel *)[cell.contentView viewWithTag:kType];
    	typeLbl.text = [theObj valueForKey:@"type"];
    	
    	UILabel *priceLbl = (UILabel *)[cell.contentView viewWithTag:kPrice];
    	NSNumber *nPrice = [theObj valueForKey:@"price"];
    	priceLbl.text = [NSString stringWithFormat:@"¥%d",[nPrice intValue]];
    	
    	UILabel *introLbl = (UILabel *)[cell.contentView viewWithTag:kIntro];
    	NSString *introText = [theObj valueForKey:@"intro"];
    	if([introText length] > 70){
    		introText = [introText substringToIndex:70];
    		introText = [introText stringByAppendingString:@"..."];
    	}
    	introLbl.text = introText;	
        return cell;
    }

    上面的代码中,由于坐着并没有为它的tableViewCell加一个id,所以当第一次创建cell的时候,是空的,需要自己定制,定制的过程就是把各个控件位置要放好,这个我没试过,所以对上面的坐标没什么感觉,这个需要多多尝试才知道。

    这里要说的有几个点:(1)、tableViewCell有一个属性是contentView是用来容纳所有在他上面出现的控件的,自己绘制cell的时候,一定要把控件加上这个view上面,然后还有一个backgroundView是所有子view的最后面那个,在contentView上面。

    (2)、本例中,cell的背景是自绘的,我不会,所以TableCellView类的代码看不太懂,有兴趣的朋友自己去研究下。

    (3)、UIView有个属性tag,还有一个方法是viewWithTag.

    tag是一个自己定义的常数,用来标记一个对象。viewWithTag方法呢,就是返回匹配给定的tag的view。

    1. -(IBAction) add:(id)sender {  
    2.     AddController *addC = [[AddController alloc] initWithNibName:@"AddController" bundle:nil];  
    3.     [self.navigationController pushViewController:addC animated:YES];  
    4.     //self.navigationController.navigationBarHidden = NO;  
    5.     [addC release];  
    6. }  
    -(IBAction) add:(id)sender {
    	AddController *addC = [[AddController alloc] initWithNibName:@"AddController" bundle:nil];
    	[self.navigationController pushViewController:addC animated:YES];
    	//self.navigationController.navigationBarHidden = NO;
    	[addC release];
    }

    上面的代码是界面中右上角i按钮的响应方法,可以看到上面这种实现navigation跳转的方法是用push 对应的在返回的时候就用pop。这是在segue出现前的一个实现跳转的方法,现在的话 有了segue,就不用这样做咯。

    这个就是按完i按钮后来到的添加新菜系的界面xib文件。

    由于我主要是讲我的收获,所以在这个界面我只想讲怎么往coredata中添加新元组咯

    1. -(IBAction) add:(id)sender {  
    2.     if([name.text length]*[price.text length]*[type.text length]*[image.text length]*[intro.text length] == 0){  
    3.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"All fields needed!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];  
    4.         [alert show];  
    5.         [alert release];  
    6.     }else{  
    7.           
    8.         HotelAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  
    9.         //connection  
    10.         NSManagedObjectContext *context = [appDelegate managedObjectContext];  
    11.         //table   
    12.         NSEntityDescription *entityDescr =[NSEntityDescription entityForName:@"Dishes" inManagedObjectContext:context];  
    13.         //query  
    14.         NSFetchRequest *request = [[NSFetchRequest alloc] init];  
    15.         [request setEntity:entityDescr];  
    16.         //pred  
    17.         //NSPredicate *pred = [NSPredicate predicateWithString:@"id=1"];  
    18.           
    19.         NSError *error;  
    20.         NSManagedObject *aDish = nil;  
    21.         NSNumber *id = [NSNumber numberWithInt:1];  
    22.         NSArray *objects = [context executeFetchRequest:request error:&error];  
    23.         if(objects != nil){  
    24.             aDish = [objects lastObject];  
    25.             id = [aDish valueForKey:@"id"];  
    26.             id = [NSNumber numberWithInt:[id intValue]+1];  
    27.   
    28.         }  
    29.           
    30.         aDish = [NSEntityDescription insertNewObjectForEntityForName:@"Dishes" inManagedObjectContext:context];  
    31.         [aDish setValue:id forKey:@"id"];  
    32.         [aDish setValue:name.text forKey:@"name"];  
    33.         [aDish setValue:[NSNumber numberWithInt:[price.text intValue]] forKey:@"price"];  
    34.         [aDish setValue:type.text forKey:@"type"];  
    35.         [aDish setValue:image.text forKey:@"image"];  
    36.         [aDish setValue:intro.text forKey:@"intro"];  
    37.         [request release];  
    38.         [context save:&error];  
    39.         [self clearFields];  
    40.     }  
    41. }  
    -(IBAction) add:(id)sender {
    	if([name.text length]*[price.text length]*[type.text length]*[image.text length]*[intro.text length] == 0){
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"All fields needed!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    		[alert show];
    		[alert release];
    	}else{
    		
    		HotelAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    		//connection
    		NSManagedObjectContext *context = [appDelegate managedObjectContext];
    		//table 
    		NSEntityDescription *entityDescr =[NSEntityDescription entityForName:@"Dishes" inManagedObjectContext:context];
    		//query
    		NSFetchRequest *request = [[NSFetchRequest alloc] init];
    		[request setEntity:entityDescr];
    		//pred
    		//NSPredicate *pred = [NSPredicate predicateWithString:@"id=1"];
    		
    		NSError *error;
    		NSManagedObject *aDish = nil;
    		NSNumber *id = [NSNumber numberWithInt:1];
    		NSArray *objects = [context executeFetchRequest:request error:&error];
    		if(objects != nil){
    			aDish = [objects lastObject];
    			id = [aDish valueForKey:@"id"];
    			id = [NSNumber numberWithInt:[id intValue]+1];
    
    		}
    		
    		aDish = [NSEntityDescription insertNewObjectForEntityForName:@"Dishes" inManagedObjectContext:context];
    		[aDish setValue:id forKey:@"id"];
    		[aDish setValue:name.text forKey:@"name"];
    		[aDish setValue:[NSNumber numberWithInt:[price.text intValue]] forKey:@"price"];
    		[aDish setValue:type.text forKey:@"type"];
    		[aDish setValue:image.text forKey:@"image"];
    		[aDish setValue:intro.text forKey:@"intro"];
    		[request release];
    		[context save:&error];
    		[self clearFields];
    	}
    }

    上面的代码是add按钮的响应方法,看完就知道了咯。

    这个界面要讲的就是view的动画。这个程序中,左右滑动,可以实现翻页,看上一道菜或下一道菜,这个对我来说很有用。

    1. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    2.     //int count = [touches count];  
    3.     //NSLog(@"count=%d",count);  
    4.     UITouch *touch = [touches anyObject];  
    5.     CGPoint location = [touch locationInView:self.view];  
    6.     self.lastLoction = location;  
    7.     moveChanged = NO;  
    8. }  
    9. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{  
    10.   
    11. }  
    12. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    13.     CGPoint location = [[touches anyObject] locationInView:self.view];  
    14.     lastLoction = location;  
    15. }  
    16. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
    17.     UITouch *touch = [touches anyObject];  
    18.     CGPoint location = [touch locationInView:self.view];  
    19.     if(!moveChanged && fabs(location.x - lastLoction.x) > 60 && fabs(location.y - lastLoction.y) < 25){  
    20.         moveChanged = YES;  
    21.         [UIView beginAnimations:nil context:imageView];  
    22.         [UIView setAnimationDuration:1.0];  
    23.         [UIView setAnimationTransition:( location.x - lastLoction.x > 0? UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown) forView:self.view cache:YES];  
    24.         [UIView setAnimationDelegate:self];  
    25.         [UIView setAnimationDidStopSelector:@selector(onAnimationStop:finished:context:)];  
    26.         [UIView commitAnimations];  
    27.           
    28.         NSUInteger count = [self.managedObjects count];  
    29.           
    30.         if(location.x - lastLoction.x >0){//right  
    31.             if(currentRowIndex < count-1){  
    32.                 currentRowIndex = currentRowIndex +1;  
    33.             }  
    34.         }else{//left  
    35.             if(currentRowIndex > 0){  
    36.                 currentRowIndex = currentRowIndex -1;  
    37.             }  
    38.         }  
    39.           
    40.         self.theObj = [self.managedObjects objectAtIndex:currentRowIndex];  
    41.         NSNumber *id = [self.theObj valueForKey:@"id"];  
    42.         NSLog(@"%d",[id intValue]);  
    43.         NSLog(@"distance:%f",location.x - lastLoction.x);  
    44.           
    45.         self.imageView.image =  [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",[id intValue]]];  
    46.           
    47.         NSMutableString *title = [[NSMutableString alloc] init];  
    48.         [title appendString: [theObj valueForKey:@"name"]];  
    49.         [title appendString:@"  "];  
    50.         [title appendString:[theObj valueForKey:@"type"]];  
    51.         NSNumber *price = [theObj valueForKey:@"price"];  
    52.         [title appendString:[NSString stringWithFormat:@"¥%d",[price intValue]]];  
    53.         self.nameLbl.text = title;  
    54.         self.introText.text = [theObj valueForKey:@"intro"];          
    55.         [title release];  
    56.           
    57.         NSDictionary *theRow = [FileController readRow:currentRowIndex];  
    58.         NSUInteger orderCount = 0;  
    59.         if(theRow != nil){  
    60.             orderCount = [[theRow valueForKey:@"count"] intValue];  
    61.         }  
    62.           
    63.         self.countLbl.text = [NSString stringWithFormat:@"%d",orderCount];  
    64.   
    65.         lastLoction = location;  
    66.     }  
    67.       
    68. }  
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	//int count = [touches count];
    	//NSLog(@"count=%d",count);
    	UITouch *touch = [touches anyObject];
    	CGPoint location = [touch locationInView:self.view];
    	self.lastLoction = location;
    	moveChanged = NO;
    }
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    	CGPoint location = [[touches anyObject] locationInView:self.view];
    	lastLoction = location;
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	UITouch *touch = [touches anyObject];
    	CGPoint location = [touch locationInView:self.view];
    	if(!moveChanged && fabs(location.x - lastLoction.x) > 60 && fabs(location.y - lastLoction.y) < 25){
    		moveChanged = YES;
    		[UIView beginAnimations:nil context:imageView];
    		[UIView setAnimationDuration:1.0];
    		[UIView setAnimationTransition:( location.x - lastLoction.x > 0? UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown) forView:self.view cache:YES];
    		[UIView setAnimationDelegate:self];
    		[UIView setAnimationDidStopSelector:@selector(onAnimationStop:finished:context:)];
    		[UIView commitAnimations];
    		
    		NSUInteger count = [self.managedObjects count];
    		
    		if(location.x - lastLoction.x >0){//right
    			if(currentRowIndex < count-1){
    				currentRowIndex = currentRowIndex +1;
    			}
    		}else{//left
    			if(currentRowIndex > 0){
    				currentRowIndex = currentRowIndex -1;
    			}
    		}
    		
    		self.theObj = [self.managedObjects objectAtIndex:currentRowIndex];
    		NSNumber *id = [self.theObj valueForKey:@"id"];
    		NSLog(@"%d",[id intValue]);
    		NSLog(@"distance:%f",location.x - lastLoction.x);
    		
    		self.imageView.image =	[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",[id intValue]]];
    		
    		NSMutableString *title = [[NSMutableString alloc] init];
    		[title appendString: [theObj valueForKey:@"name"]];
    		[title appendString:@"  "];
    		[title appendString:[theObj valueForKey:@"type"]];
    		NSNumber *price = [theObj valueForKey:@"price"];
    		[title appendString:[NSString stringWithFormat:@"¥%d",[price intValue]]];
    		self.nameLbl.text = title;
    		self.introText.text = [theObj valueForKey:@"intro"];		
    		[title release];
    		
    		NSDictionary *theRow = [FileController readRow:currentRowIndex];
    		NSUInteger orderCount = 0;
    		if(theRow != nil){
    			orderCount = [[theRow valueForKey:@"count"] intValue];
    		}
    		
    		self.countLbl.text = [NSString stringWithFormat:@"%d",orderCount];
    
    		lastLoction = location;
    	}
        
    }
    1. [UIView beginAnimations:nil context:imageView];  
    2. [UIView setAnimationDuration:1.0];  
    3. [UIView setAnimationTransition:( location.x - lastLoction.x > 0? UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown) forView:self.view cache:YES];  
    4. [UIView setAnimationDelegate:self];  
    5. [UIView setAnimationDidStopSelector:@selector(onAnimationStop:finished:context:)];  
    6. [UIView commitAnimations];  
    		[UIView beginAnimations:nil context:imageView];
    		[UIView setAnimationDuration:1.0];
    		[UIView setAnimationTransition:( location.x - lastLoction.x > 0? UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown) forView:self.view cache:YES];
    		[UIView setAnimationDelegate:self];
    		[UIView setAnimationDidStopSelector:@selector(onAnimationStop:finished:context:)];
    		[UIView commitAnimations];

    截取的这几句代码就是一个动画的过程.

    第二个标签是读取已经下单的菜,这个没什么好说的 和第一个标签的第一个页面很相似。

    第三个界面是一个可以发表建议的界面,用到了segment controller来分两个界面,一个是写的 一个是看全部的历史建议的。

    切换segmentController上面的标签时,会响应valueChange方法,可以在里面判断是要显示哪个界面。

    POST请求的发送也可以再复习一下:

    1. NSString *postMsg =[NSString stringWithFormat:@"user=%@&content=%@",self.name.text,self.textView.text];  
    2. NSData *postData = [postMsg dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];  
    3. NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];  
    4.   
    5. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://bula.kilu.net/post.php"]];  
    6. [request setHTTPMethod:@"POST"];  
    7. [request setValue:postLength forHTTPHeaderField:@"Content-Length"];  
    8. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];  
    9. [request setHTTPBody:postData];  
    10.   
    11. [NSURLConnection connectionWithRequest:request delegate:self];  
    	NSString *postMsg =[NSString stringWithFormat:@"user=%@&content=%@",self.name.text,self.textView.text];
    	NSData *postData = [postMsg dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    	NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    	
    	NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://bula.kilu.net/post.php"]];
    	[request setHTTPMethod:@"POST"];
    	[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    	[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    	[request setHTTPBody:postData];
    
    	[NSURLConnection connectionWithRequest:request delegate:self];


    好了 我学到的东西是这些,还有一些是我看不懂的 对于自己绘制一些view的方法我还是不会,需要补补。

    源码我待会会上传,有兴趣的朋友下载去研究下咯 不错的学习资料。,如果运行的时候说差哪一张图片的什么 就随便放张进去文件夹里面然后按它说的给个名字就行了,不影响代码研究。

    上传好了 源码地址:ios点餐系统

  • 相关阅读:
    break-continue
    函数定义
    函数类型
    为何要继承SpringBootServletInitializer,为何要实现configure这方法
    查询一个表中的两个字段值相同的数据
    数据库中查出来的时间多8小时&查询数据正常展示少8小时
    @JsonFormat与@DateTimeFormat注解的使用
    用js获取当前月份的天数
    js获取当前年,月,日,时,分,秒
    maven配置和安装
  • 原文地址:https://www.cnblogs.com/yulang314/p/3550324.html
Copyright © 2011-2022 走看看