2012年12月14日 星期五

Core Animation的基本使用(十五)

CATransform3D的矩陣運算提供了切變轉換的功能。CATransform3D矩陣的資料可參考Core Animation的基本使用(十二)。我們只改變m21(X方向切變)及m12的值(Y方向切變)。變化值從-1~ 1。

切變在App上的實用性還不確定,僅作實驗瞭解其性質。


1. 實作沿用前一個專案,在storyboard上入兩個slide及Label,另外再加上一個button來啓動轉換。

2. 在mainViewController.h加上所需元件註冊。
@property (weak, nonatomic) IBOutlet UISlider *xDistortValueSlide;
@property (weak, nonatomic) IBOutlet UISlider *yDistortValueSlide;
@property (weak, nonatomic) IBOutlet UILabel *xDistortValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *yDistortValueLabel;


3. 在mainViewController.m加上控制程式碼

@synthesize xDistortValueLabel;
@synthesize xDistortValueSlide;
@synthesize yDistortValueLabel;
@synthesize yDistortValueSlide;

//float inputvalue;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    transform3DLayer = [CALayer layer];
    transform3DLayer.bounds = CGRectMake(0, 0, 250, 250);
    transform3DLayer.position = CGPointMake(350, 350);
    transform3DLayer.contents = (id)[UIImage imageNamed:@"TaiwanMap.jpg"].CGImage;
   
    perspective = CATransform3DIdentity;
   
    transform3DLayer.anchorPoint = CGPointMake(0.0, 1.0);
   
    [self.view.layer addSublayer:transform3DLayer];
   
   
    animationValueSlider.maximumValue =200;
    animationValueSlider.minimumValue =-200;
    animationValueSlider.value = 100;
    animationValueLabel.text = [NSString stringWithFormat:@"移動%2.2f點",animationValueSlider.value];
   
    zDistanceValue.value = 200;
   
    zDistanceLabel.text = [NSString stringWithFormat:@"Z-Dis=%2.2f",zDistanceValue.value];
   
    xDistortValueSlide.value = 0;
    yDistortValueSlide.value = 0;
   
    xDistortValueLabel.text = [NSString stringWithFormat:@"X切變量 = %2.2f", xDistortValueSlide.value];
    yDistortValueLabel.text = [NSString stringWithFormat:@"Y切變量 = %2.2f", yDistortValueSlide.value];


   
}





- (IBAction)distortTransform:(id)sender {
   
    //transform3DLayer.transform = CGAffineTransformMakeDistort(0.3, 0.3);
   
    perspective = CATransform3DIdentity;
   
   // transform3DLayer.transform = CATransform3DConcat(perspective, CGAffineTransformMake(1, 0.3, 0.3, 1, 0, 0));

    perspective.m21 = xDistortValueSlide.value;  // X distort
   
    perspective.m12 = yDistortValueSlide.value;  // Y distort
   
    CATransform3D scale = CATransform3DIdentity;
   
    transform3DLayer.transform = CATransform3DConcat(perspective, scale);
   
}
- (IBAction)xDistortValueChange:(id)sender {
   
    xDistortValueLabel.text = [NSString stringWithFormat:@"X切變量 = %2.2f", xDistortValueSlide.value];
}
- (IBAction)yDistortValueChange:(id)sender {
   
    yDistortValueLabel.text = [NSString stringWithFormat:@"Y切變量 = %2.2f", yDistortValueSlide.value];

}



4. 執行結果







沒有留言: