2011-12-20 46 views
-1

我想要的是,當我滾動iPad時,圖像會向上/向下隱藏。效果應該像如何在IOS中對圖像進行盲目效果?

http://madrobby.github.com/scriptaculous/combination-effects-demo/盲目演示。

我該怎麼做?

我嘗試了Apple的反射示例,但由於我應該在每個陀螺儀動作中重繪圖像,所以出現性能問題。

這裏是代碼:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
tmp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"galata2.jpg"]]; 
// Do any additional setup after loading the view, typically from a nib. 
NSUInteger reflectionHeight = imageView1.bounds.size.height * 1; 


imageView1 = [[UIImageView alloc] init]; 
imageView1.image = [UIImage imageNamed:@"galata1.jpg"]; 
[imageView1 sizeToFit]; 
[self.view addSubview:imageView1]; 

imageView2 = [[UIImageView alloc] init]; 
//UIImageView *tmp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"galata2.jpg"]]; 
imageView2.image = [UIImage imageNamed:@"galata2.jpg"]; 

[imageView2 sizeToFit]; 



[self.view addSubview:imageView2]; 

motionManager = [[CMMotionManager alloc] init]; 
motionManager.gyroUpdateInterval = 1.0/10.0; 
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] 
            withHandler: ^(CMDeviceMotion *motion, NSError *error){ 

             [self  performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES]; 

            }]; 

} 

////

- (void)handleDeviceMotion:(CMDeviceMotion*)motion{ 
CMAttitude *attitude = motion.attitude; 
int rotateAngle = abs((int)degrees(attitude.roll)); 
//CMRotationRate rotationRate = motion.rotationRate; 
NSLog(@"rotation rate = [Pitch: %f, Roll: %d, Yaw: %f]", degrees(attitude.pitch), abs((int)degrees(attitude.roll)), degrees(attitude.yaw)); 
int section = (int)(rotateAngle/30); 
int x = rotateAngle % 30; 

NSUInteger reflectionHeight = (1024/30)*x; 
NSLog(@"[x = %d]", reflectionHeight); 
imageView2.image = [self reflectedImage:tmp withHeight:reflectionHeight]; 
} 

////要做到這一點

- (UIImage *)reflectedImage:(UIImageView *)fromImage withHeight:(NSUInteger)height 
{ 
if(height == 0) 
    return nil;  


// create a bitmap graphics context the size of the image 
CGContextRef mainViewContentContext = MyCreateBitmapContext(fromImage.bounds.size.width, fromImage.bounds.size.height); 


// create a 2 bit CGImage containing a gradient that will be used for masking the 
// main view content to create the 'fade' of the reflection. The CGImageCreateWithMask 
// function will stretch the bitmap image as required, so we can create a 1 pixel wide gradient 
CGImageRef gradientMaskImage = CreateGradientImage(1, kImageHeight); 

// create an image by masking the bitmap of the mainView content with the gradient view 
// then release the pre-masked content bitmap and the gradient bitmap 
CGContextClipToMask(mainViewContentContext, CGRectMake(0.0, 0.0, fromImage.bounds.size.width,height), gradientMaskImage); 
CGImageRelease(gradientMaskImage); 

// In order to grab the part of the image that we want to render, we move the context origin to the 
// height of the image that we want to capture, then we flip the context so that the image draws upside down. 
//CGContextTranslateCTM(mainViewContentContext, 0.0,0.0); 
//CGContextScaleCTM(mainViewContentContext, 1.0, -1.0); 


// draw the image into the bitmap context 
CGContextDrawImage(mainViewContentContext, CGRectMake(0, 0, fromImage.bounds.size.width, fromImage.bounds.size.height), fromImage.image.CGImage); 

// create CGImageRef of the main view bitmap content, and then release that bitmap context 
CGImageRef reflectionImage = CGBitmapContextCreateImage(mainViewContentContext); 
CGContextRelease(mainViewContentContext); 

// convert the finished reflection image to a UIImage 
UIImage *theImage = [UIImage imageWithCGImage:reflectionImage]; 

// image is retained by the property setting above, so we can release the original 
CGImageRelease(reflectionImage); 

return theImage; 
} 
+0

我不知道你到目前爲止做了什麼 - 但是你看*看了Core Animation文檔嗎? – Abizern 2011-12-20 20:01:49

回答

1

的一種方法是使用另一個覆蓋認爲,通過動畫逐漸改變高度;

如果你有一個名爲要覆蓋theView視圖,嘗試這樣的事情透露theView封面視圖下:

UIView *coverView = [UIView alloc] initWithFrame:theView.frame]; 
coverView.backgroundcolor = [UIColor whiteColor]; 
[theView.superView addSubView:coverView]; // this covers theView, adding it to the same view that the view is contained in; 
CGRect newFrame = theView.frame; 
newFrame.size.height = 0; 
newFrame.origin.y = theView.origin.y + theView.size.height; 
[UIView animateWithDuration:1.5 
         delay: 0.0 
        options: UIViewAnimationOptionRepeat 
       animations:^{ 
        coverView.frame = newFrame; 
       } 
       completion:nil 
       ]; 

這應該涵蓋視圖,然後通過改變框架OV揭示它在改變高度的同時將蓋子向下移動。

我還沒有試過這個代碼,但是這是一個方向,你可以創建盲目的效果。我經常使用類似的代碼,這很容易處理。另外,它不需要知道核心動畫。

+0

但它是一個動畫。我不想動畫。我想要的是,當我滾動設備時,在陀螺儀的幫助下,計算高度,並用面罩重新繪製圖像。 – Burak 2011-12-20 20:35:57

+0

我很抱歉。我唯一想要的就是用alpha漸變蒙版圖像,並使用滾動操作更改此漸變的高度。所以它會像一張光柵照片。 – Burak 2011-12-20 21:32:05

+0

你可能想要修改你的問題並作一些澄清。 – Jim 2011-12-20 22:47:38

相關問題