2012-08-14 52 views
0

我有一個向右移動圖像的動畫,然後當我放開按鈕時停止。我需要圖像的幀在不同的時間是不同的,所以我用CGRect變量代替myImageView.frame = CGRectMake。但是當我使用可變圖像跳躍回到原來的位置時,每次我放開,而不是像往常一樣停下來。如果使用CGRect變量代替CGRectMake,動畫不起作用

if (myInt == 2) { 
    MyCGRect = CGRectMake(myImageView.frame.origin.x,myImageView.frame.origin.y,18,42); 
} else { 
    MyCGRect = CGRectMake(myImageView.frame.origin.x,myImageView.frame.origin.y,35,28); 
} 

-(IBAction)holdToMakeImageMove:(id)sender 
{ 
//!!!!!This Works!!!!!! 
myImageView.frame = CGRectMake(myImageView.frame.origin.x,myImageView.frame.origin.y,18,42); 

//!!!!!!THIS doesnt work 

myImageView.frame = MyCGRect; 

//...animation 

} 
+3

您是否在調試器中檢查了「MyCGRect」的值?也許它與'frame'值相同? – Stream 2012-08-14 21:20:27

+0

@Stream我做了,事實證明myCGRect總是停留在同一個地方。它不應該改變,如果它有myImageView.frame.origin.x? – 2012-08-14 22:58:06

回答

-1

例如,此代碼適用於我。將按鈕幀從(0,800,50,50)更改爲(0,0,100,100)

@interface ViewController() 
... 
@property (nonatomic, assign) CGRect myRect; 
... 
@end 

@implementation ViewController 
... 
@synthesize myRect = _myRect; 
... 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btn setFrame:CGRectMake(0, 800, 50, 50)]; 
    [btn addTarget:self action:@selector(avvia:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:btn]; 

    self.myRect = CGRectMake(100, 0, 100, 100); 
} 
... 
- (void)avvia:(id)sender { 
    ((UIButton *)sender).frame = self.myRect; 
} 
... 
@end 
+0

圖像變化,但我的圖像仍然被拖回去? – 2012-08-14 21:50:56

+0

WhiteTiger正在讓它變得複雜。坦納如果你願意我可以修復你的代碼,如果你給我發電子郵件,只需.h和.m是必要的(但整個項目將有助於測試和調試)。 – Comradsky 2012-08-14 21:56:35

+0

我很抱歉,但我只做了一個使用示例,將代碼放在恰當的位置。 – WhiteTiger 2012-08-14 22:13:44