Ios 放下图像,当它超出边界时释放
我是Objective-C编程新手,目前对下面的代码有问题。我试图让一个图像下降时,一个按钮被触摸,它只是冻结时,试图这样做。它需要放下图像,直到它到达屏幕的底部,然后它应该被释放Ios 放下图像,当它超出边界时释放,ios,objective-c,Ios,Objective C,我是Objective-C编程新手,目前对下面的代码有问题。我试图让一个图像下降时,一个按钮被触摸,它只是冻结时,试图这样做。它需要放下图像,直到它到达屏幕的底部,然后它应该被释放 - (IBAction)itemDrop:(UIButton *)sender{ if (strncmp([[sender currentTitle] UTF8String], [copyPrimaryArray[0] UTF8String], 2) == 0) { UIImage *bubblImage
- (IBAction)itemDrop:(UIButton *)sender{
if (strncmp([[sender currentTitle] UTF8String], [copyPrimaryArray[0] UTF8String], 2) == 0)
{
UIImage *bubblImage = [UIImage imageNamed:@"Red_apple.png"];
UIImageView *appleImg = [[UIImageView alloc] initWithImage:bubblImage];
appleImg.translatesAutoresizingMaskIntoConstraints = NO;
[appleImg setContentMode:UIViewContentModeScaleAspectFit];
[appleImg sizeToFit];
[self.view addSubview:appleImg];
NSLog(@"You clicked the Apple");
int i = 0;
CGPoint coordinates = sender.frame.origin;
CGFloat x = coordinates.x;
CGFloat y = coordinates.y;
CGFloat z = UIScreen.mainScreen.bounds.size.height;
while (y < z) {
[UIView beginAnimations:nil context:nil];
appleImg.center = CGPointMake(x, y+i);
[UIView commitAnimations];
i++;
}
}
}
-(iAction)项删除:(UIButton*)发送方{
如果(strncmp([[sender currentTitle]UTF8String],[copyPrimaryArray[0]UTF8String],2)=0)
{
UIImage*bubblImage=[UIImage ImageName:@“Red_apple.png”];
UIImageView*AppleMg=[[UIImageView alloc]initWithImage:BubbliImage];
appleImg.translatesAutoResistingMaskintoConstraints=否;
[appleImg setContentMode:UIViewContentModeScaleAspectFit];
[appleImg sizeToFit];
[self.view addSubview:appleImg];
NSLog(@“你点击了苹果”);
int i=0;
CGPoint坐标=sender.frame.origin;
CGFloat x=坐标.x;
cgy=坐标.y;
CGFloat z=UIScreen.mainScreen.bounds.size.height;
而(y
您无需手动一次将图像视图移动一个点。这就是我从您使用while循环中所理解的。Objective-c为你做了很多事情
UIImage *bubblImage = [UIImage imageNamed:@"Red_apple.png"];
UIImageView *appleImg = [[UIImageView alloc] initWithImage:bubblImage];
// Set the frame of the original position here, for example:
CGRect originalPosition = CGRectMake(0, 0, appleImg.frame.size.width, appleImg.frame.size.height);
appleImg.frame = originalPosition;
// Then animate it to the new position:
[UIView animateWithDuration:1.0
animations:^{
CGRect newPosition = CGRectMake(0, 640, appleImg.frame.size.width, appleImg.frame.size.height);
appleImg.frame = newPosition;
}
completion:^(BOOL finished) {
NSLog(@"completion block"); // You can set the completion block to nil if you have nothing to do here.
}];
确保开始使用基于块的动画。
自从iOS 4.0以来,使用UIView的beginAnimations:context是非常不受欢迎的。非常感谢,这完美地解决了这个问题。没问题,这就是这个网站的用途。我想补充一点,如果([sender.currentTitle isEqualToString:copyPrimaryArray[0]])可以使用
而不是其中的C代码。