gpt4 book ai didi

iphone - 我如何将 UIImage 堆叠在一起并在 UIKit 中摇摆不定

转载 作者:可可西里 更新时间:2023-11-01 04:44:25 25 4
gpt4 key购买 nike

我想知道这个应用程序如何在 UIKit 中堆叠 uiimage 和 wavering。

我正在关注这个 xcode: Using a UIImageView subclass with NSTimers 让图像随机下降,但我如何捕捉它并将 UIImage 堆叠在一起?

enter image description here

感谢您的回复...编辑:为清楚起见

https://www.youtube.com/watch?v=2dpZCoi4xFk 40 秒后将展示它如何堆叠在一起并随加速度计摆动

这将模拟 UIIimageView 的下降

- (void)viewDidLoad {
[super viewDidLoad];

// set the background color to something COLD
self.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0];

// load our ph image we will use the same image over and over
phImage = [UIImage imageNamed:@"placeholder.png"];

// start a timet that will fire 1 times per second
[NSTimer scheduledTimerWithTimeInterval:(1) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

// Timer event is called whenever the timer fires
- (void)onTimer
{
// build a view from our ph image
UIImageView* placeholderView = [[UIImageView alloc] initWithImage:phImage];

// use the random() function to randomize up our ph attributes
//int startX = round(random() % 300);
int startX = round(random() % 275);
//int endX = round(random() % 300);
int endX = startX;

double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 50) + 1.0;

// set the PH start position
phView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
phView.alpha = 0.25;

// put the ph in our main view
[self.view addSubview:phView];

[UIView beginAnimations:nil context:phView];
// set up how fast the ph will fall
[UIView setAnimationDuration:5 * speed];

// set the postion where ph will move to
phView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);

// set a stop callback so we can cleanup the ph when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

}
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

UIImageView *phView = context;
[phView removeFromSuperview];
NSLog(@"[phView retainCount] = %d",[phView retainCount]);

}

感谢阅读和评论。

最佳答案

对于堆叠,只需制作一个 UIView 将所有放置的图像放在一起。

UIView *stackView = [UIView new];

-(void) addView: (UIView*) view toStack: (UIView*) stackView
{

if ([stackView subviews].count > 0)
{
CGRect frame = [[[stackView subviews] lastObject] frame];
view.frame = CGRectOffset(frame, 0, 20);
}
[stackView addSubview:view];
}

对于倾斜,设置加速度计并使用 x 和 y 来确定倾斜度。

-(void)configureAccelerometer
{
UIAccelerometer* theAccelerometer = [UIAccelerometer sharedAccelerometer];
theAccelerometer.updateInterval = 0.20;

theAccelerometer.delegate = self;
// Delegate events begin immediately.
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
UIAccelerationValue x, y, z;
x = acceleration.x;
y = acceleration.y;

//[stackView tiltForX: x Y: y]

}

有关 Motion Events 的更多信息

对于碰撞检测,你可以做类似的事情

if (CGRectIntersectsRect([[[stackView subviews] lastObject] frame], fallingView.frame))
[self addView: fallingView toStack: stackView];

关于iphone - 我如何将 UIImage 堆叠在一起并在 UIKit 中摇摆不定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com