从我的问题开始,您将在大多数购物应用程序(如 Amazon、Flip kart 等)中构建一个功能,如添加到购物车。
- (void)viewDidLoad {
[super viewDidLoad];
shoeImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(46,222, 51 , 51)];
shoeImageView1.image = [UIImage imageNamed:@"shoe.png"];
shoeImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(150,222, 51 , 51)];
shoeImageView2.image = [UIImage imageNamed:@"shoe1.png"];
shoeImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(225,222, 51 , 51)];
shoeImageView3.image = [UIImage imageNamed:@"shoe2.png"];
addTOCart = [[UIImageView alloc]initWithFrame:CGRectMake(132,400, 80, 80)];
addTOCart.image = [UIImage imageNamed:@"basket.png"];
[self.view addSubview:addTOCart];
imageViewArray = [[NSMutableArray alloc]initWithObjects: shoeImageView1,shoeImageView2 ,shoeImageView3,nil];
for (int i=0; i<imageViewArray.count; i++) {
[self.view addSubview:[imageViewArray objectAtIndex:i]];
[[imageViewArray objectAtIndex:i]setUserInteractionEnabled:YES];
//[self touchesBegan:imageViewArray[i] withEvent:nil];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for(int i=0 ; i< imageViewArray.count; i++)
{
CGPoint pt = [[touches anyObject]locationInView:self.view];
startLocation = pt;
newtemp = [imageViewArray objectAtIndex:i];
UITouch* bTouch = [touches anyObject];
if ([bTouch.view isEqual:newtemp])
{
firstTouchPoint = [bTouch locationInView:[self view]];
oldX = firstTouchPoint.x - [[bTouch view]center].x;
oldY = firstTouchPoint.y - [[bTouch view]center].y;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for(int i=0 ; i< imageViewArray.count; i++)
{
newtemp = [imageViewArray objectAtIndex:i];
//oldLoc = newtemp.frame;
if (CGRectContainsPoint(addTOCart.frame , newtemp.frame.origin))
{
NSLog(@"touched");
dragging = NO;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Cart" message:@"Added to Cart" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[[imageViewArray objectAtIndex:i] setImage:[UIImage imageNamed:@""]];
[imageViewArray removeObjectAtIndex:i];
[alert show];
break;
}
else
{
//[newtemp setCenter:CGPointMake(startLocation.x-oldX, startLocation.y-oldY)];
}
// self.view.userInteractionEnabled= NO;
}
dragging = NO;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* mTouch = [touches anyObject];
// if ([mTouch.view isEqual: newtemp]) {
CGPoint cp = [mTouch locationInView:[self view]];
[[mTouch view]setCenter:CGPointMake(cp.x-oldX, cp.y-oldY)];
// }
}
您可以使用这些代码来实现这些功能。使用这些代码,您可以将对象拖动到屏幕中的任何位置。
对于示例项目,您可以使用此链接。