我想要完成的是将UIImageView移动到iPhone屏幕的顶部边缘然后继续向上移动,但是一旦图像通过顶部边缘,让它出现在屏幕的底部,因为它继续通过。 也是右倾和右转
它将通过Accelerometer和UISwipeGesture进行移动 我使用了两个相同的图像,但没有正常工作 如果有人有例子,请分享
可以使用一张图片吗?
如果有人知道这个任务,请告诉我。
感谢。
答案 0 :(得分:0)
使用Accelerometer
你必须使用两个相同的图像
这里我正在使用1)_imageView 2)_imgViewTemp
- (void)startUpdateAccelerometer:(UIImageView*)ImgView1 view:(UIView*)boundView
{
if ([self.mManager isAccelerometerAvailable] == YES) {
// self.mManager.accelerometerUpdateInterval = 1.0/10000.0;
[self.mManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
{
CGRect imgFrame = _imageView.frame;
if(isWindowActive){
imgFrame.origin.x -=accelerometerData.acceleration.y*8.0 ;
if(!CGRectContainsRect(self.view.bounds, imgFrame))
imgFrame.origin.x = _imageView.frame.origin.x;
imgFrame.origin.y -=accelerometerData.acceleration.x *8.0;
if(!CGRectContainsRect(self.view.bounds, imgFrame))
imgFrame.origin.y = _imageView.frame.origin.y;
_imageView.frame = imgFrame;
}
else{
imgFrame.origin.x -=accelerometerData.acceleration.y*8.0 ;
imgFrame.origin.y -=accelerometerData.acceleration.x *8.0;
if(CGRectContainsRect(boundView.frame, _imageView.frame)){
_imgViewTemp.hidden=YES;
}
_imageView.frame=imgFrame;
if ((_imageView.frame.origin.y>_bounderView.frame.size.height-_imageView.frame.size.height && _imageView.frame.origin.x>_bounderView.frame.size.width-_imageView.frame.size.width) ){
NSLog(@"-x hidden");
_imageView.frame=CGRectMake(_imageView.frame.origin.x, 0, _imageView.frame.size.width, _imageView.frame.size.height);
_imgViewTemp.frame=CGRectMake(_imgViewTemp.frame.origin.x, 0,_imageView.frame.size.width, _imageView.frame.size.height);
return ;
}
else if ((_imageView.frame.origin.y>_bounderView.frame.size.height-_imageView.frame.size.height && _imageView.frame.origin.x<0) ){
NSLog(@"-x hidden");
_imageView.frame=CGRectMake(_imageView.frame.origin.x, 0,_imageView.frame.size.width, _imageView.frame.size.height);
_imgViewTemp.frame=CGRectMake(_imgViewTemp.frame.origin.x, 0, _imageView.frame.size.width, _imageView.frame.size.height);
return ;
}
else if (_imageView.frame.origin.y<0 && _imageView.frame.origin.x>_bounderView.frame.size.width-_imageView.frame.size.width) {
_imageView.frame=CGRectMake(_imageView.frame.origin.x, _bounderView.frame.size.height-_imageView.frame.size.height,_imageView.frame.size.width, _imageView.frame.size.height);
_imgViewTemp.frame=CGRectMake(_imgViewTemp.frame.origin.x,_bounderView.frame.size.height-_imageView.frame.size.height,_imageView.frame.size.width, _imageView.frame.size.height);
return ;
}else if (_imageView.frame.origin.y<0 && _imageView.frame.origin.x<0) {
_imageView.frame=CGRectMake(_imageView.frame.origin.x, _bounderView.frame.size.height-_imageView.frame.size.height, _imageView.frame.size.width, _imageView.frame.size.height);
_imgViewTemp.frame=CGRectMake(_imgViewTemp.frame.origin.x,_bounderView.frame.size.height-_imageView.frame.size.height,_imageView.frame.size.width, _imageView.frame.size.height);
return ;
}
else if (imgFrame.origin.x<0) {
if (imgFrame.origin.x<-_imageView.frame.size.width) {
NSLog(@"less 70");
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(_bounderView.frame.size.width+imgFrame.origin.x, imgFrame.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}
}
else if (imgFrame.origin.x>_bounderView.frame.size.width-_imageView.frame.size.width){
if (imgFrame.origin.x>_bounderView.frame.size.width) {
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(-(_bounderView.frame.size.width-imgFrame.origin.x), imgFrame.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}
}
else if (imgFrame.origin.y<0){
if (imgFrame.origin.y<-_imageView.frame.size.height) {
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(imgFrame.origin.x,_bounderView.frame.size.height+imgFrame.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}
}
else if (imgFrame.origin.y>_bounderView.frame.size.height-imgFrame.size.height){
if (imgFrame.origin.y>_bounderView.frame.size.height) {
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(imgFrame.origin.x,-(_bounderView.frame.size.height-imgFrame.origin.y), _imageView.frame.size.width, _imageView.frame.size.height);
}
}
}
}];
}
}
使用计时器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.004 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void)timerFired:(NSTimer *)timerData{
UIImageView *imgView=_imageView;//firstimage
NSInteger Xpos=imgView.frame.origin.x;
NSInteger Ypos=imgView.frame.origin.y;
if (movingPostion==Left) {
Xpos-=1;
}
if (movingPostion==Right) {
Xpos+=1;
}
if (movingPostion==Up) {
Ypos-=1;
}
if (movingPostion==Down) {
Ypos+=1;
}
imgView.frame = CGRectMake(Xpos, Ypos, _imageView.frame.size.width, _imageView.frame.size.height);
CGRect imgFrame = imgView.frame;
_imageView.frame=imgFrame;
if ((imgFrame.origin.y<0 && imgFrame.origin.x<0) || (imgFrame.origin.y<0 && imgFrame.origin.x>_bounderView.frame.size.width-_imageView.frame.size.width)) {
imgFrame.origin.y=_bounderView.frame.size.height+imgFrame.origin.y;
_imageView.frame=imgFrame;
return;
}else if((imgFrame.origin.y >_bounderView.frame.size.height && imgFrame.origin.x<0 ) || (imgFrame.origin.y>_bounderView.frame.size.height && imgFrame.origin.x>_bounderView.frame.size.width)){
imgFrame.origin.y=-(_bounderView.frame.size.height-imgFrame.origin.y);
_imageView.frame=imgFrame;
return;
}
else if (imgFrame.origin.x<0 ) {
if (imgFrame.origin.x<-_imageView.frame.size.width) {
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(_bounderView.frame.size.width+imgFrame.origin.x, imgFrame.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}
}
else if (imgFrame.origin.x>_bounderView.frame.size.width-_imageView.frame.size.width){
if (imgFrame.origin.x>_bounderView.frame.size.width) {
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(-(_bounderView.frame.size.width-imgFrame.origin.x), imgFrame.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}
}
else if (imgFrame.origin.y<0){
if (imgFrame.origin.y<-_imageView.frame.size.height) {
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(imgFrame.origin.x,_bounderView.frame.size.height+imgFrame.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}
}
else if (imgFrame.origin.y>_bounderView.frame.size.height-imgFrame.size.height ){
if (imgFrame.origin.y>_bounderView.frame.size.height) {
_imgViewTemp.hidden=YES;
_imageView.frame=_imgViewTemp.frame;
}else{
_imgViewTemp.hidden=NO;
_imgViewTemp.frame=CGRectMake(imgFrame.origin.x,-(_bounderView.frame.size.height-imgFrame.origin.y), _imageView.frame.size.width, _imageView.frame.size.height);
}
}