Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在屏幕侧面附近和外侧创建uiimageView_Iphone_Animation_Random_Uiimageview - Fatal编程技术网

Iphone 在屏幕侧面附近和外侧创建uiimageView

Iphone 在屏幕侧面附近和外侧创建uiimageView,iphone,animation,random,uiimageview,Iphone,Animation,Random,Uiimageview,大家好,我是法国人,请帮我学英语。我想做的是每秒在一个随机位置创建一个新图像,但是在屏幕外的一个随机位置,并且(非常重要)靠近屏幕两侧(上侧或左侧或右侧或下方)。当我想给它们设置动画时,会在屏幕外,但就在侧面周围创建动画。我该怎么做呢?此代码的作用是使用random:min:max在所需的最小和最大范围之间生成一个随机的x和y值。在onTimer函数中,我们分配一个新的UIImageView对象,然后将其中心属性设置为屏幕外的随机位置。如果If station中生成的随机数小于5,则视图将添加到

大家好,我是法国人,请帮我学英语。我想做的是每秒在一个随机位置创建一个新图像,但是在屏幕外的一个随机位置,并且(非常重要)靠近屏幕两侧(上侧或左侧或右侧或下方)。当我想给它们设置动画时,会在屏幕外,但就在侧面周围创建动画。我该怎么做呢?

此代码的作用是使用
random:min:max
在所需的最小和最大范围之间生成一个随机的x和y值。在
onTimer
函数中,我们分配一个新的
UIImageView
对象,然后将其中心属性设置为屏幕外的随机位置。如果If station中生成的随机数小于5,则视图将添加到屏幕的左侧或顶部,否则将添加到右侧或底部

/*****************************************************************************
 Generates a pseudo random CGPoint point value within the min and max range.  
 *****************************************************************************/
-(CGFloat)rand:(CGFloat)min:(CGFloat)max{
    float difference = max - min;
    return (((CGFloat) rand()/(CGFloat)RAND_MAX) * difference) + min;
}

-(void)viewDidLoad{ 
    //setup a timer property in your view controller header file
        timer = [NSTimer timerWithTimeInterval:1.0
                                    target:self 
                                  selector:@selector(onTimer) 
                                  userInfo:nil 
                                   repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

-(void)onTimer{
     UIImagView *myRandomImageView;
     myRandomImageView = [[UIImageView alloc] initWithImage:@"MyRandomImage.png"];
     if([rand:1:10] > 5)
            //put the image view to top or left side of the screen
            myRandomImageView.center = CGPointMake([rand:-100,-200], [rand:-100,-200]); 
     else 
            //put the image view to the bottom or right side of the screen
            myRandomImageView.center = CGPointMake([rand:420,520], [rand:580,680]); 
     //dont forget that the values I have eneter here to be passed to the rand:min:max 
     //function are meant to serve as a guide and that you should enter your own values to 
     //achieve the desired random view positioning off screen. 
     [self.view addSubview:myRandomImageView]; 
     [myRandomImageView release]
}

所以基本上,我对你的问题的理解是,你有一个目标:你想通过平移来制作一个物体的动画。 因此,动画从可见屏幕外部转到可见屏幕

以下是一种方法: 1.您需要确定“外部”屏幕的限制。根据UIImageView帧的大小,我认为100像素的填充是可以的。这就是x:(-100420),y:(-100580)

  • 生成随机数。在这种情况下,arc4random()优于rand()

  • 创建将由创建UIImageView的NSTimer调用的方法

  • 不要忘记正确管理UIImageView。频繁创建对象而不发布将导致应用程序崩溃。如果可能,重用它们,而不是每次调用该方法时都创建它们


  • 我的问题是关于arc4random():如何在外部屏幕侧附近进行随机数我不明白min和max的部分它们做什么?你说你不希望图像视图加载到离屏幕区域太远的地方,因此你需要min-max来定义视图随机坐标的合理允许范围。哦,好的。但是我不希望一个边的位置相同,我希望每边都有一个随机位置,不是一个常量值,不是一个常量值,它会在最小值和最大值之间生成一个随机数。如果我调用这个代码:CGPoint num=[rand:1:10];num将等于1到10之间的随机数。我们看到了吗?因此,它将根据if条件内生成的值,在屏幕左上角周围生成180度的位置,在屏幕右下角周围生成180度的位置。