English 中文(简体)
如何启动一个动作而不点击按钮?
原标题:How I can start an action without clicking to a button?

我有两个UI按钮, 我想先点击数字1 UIBton , 然后再点击数字2 >UIBton 。 在点击第二个 UIBton 后, 我要自动旋转两个 UIBton 。 我使用 CABIBimation 来旋转它们。 两个 UIBton 都有相同的 Buttonlick 动作。 这两个按钮使用不同的 CABIFIAimation 方法。

我为旋转它创建了一种新方法, 但当我从 按钮点击 动作( 与两个按钮相同) 中调用它时, 只有第一个被旋转。 为了调用旋转方法, 我使用 < code> [ 自己旋转按钮] ;

问题是如果我插入一个新的 UIButton 并给新按钮一个 IBAction 它就很好。 但我不想在图层中插入新按钮 。

我如何修补 rotate buttons , 使两个按钮 < strong> 能够同时旋转 ?

这是代码:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    if ([value isEqualToString:@"SecondImage"])
    {
        UIImageView *myView2=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton+100];
        [myView2 setHidden:FALSE];
        UIImageView *myView=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton];
        [myView setHidden:TRUE];
        CALayer *layer2 =  myView2.layer;
        layer2.anchorPoint = CGPointMake(0.5f, 0.5f);
        CABasicAnimation *animateImage2 = [CABasicAnimation animationWithKeyPath: @"transform"];
        CATransform3D transform2 = CATransform3DIdentity;
        transform2.m34 = 1.0/-900.0; 
        self.view.layer.transform = transform2;
        layer2.transform = CATransform3DMakeRotation(-M_PI/2, 0.0f, 1.0f, 0.0f);
        self.view.layer.transform = transform2;
        transform2 = CATransform3DRotate(transform2,  -M_PI, 0.0f, 1.0f, 0.0f);
        animateImage2.repeatCount = 1.0;
        animateImage2.toValue = [NSValue valueWithCATransform3D: transform2];
        animateImage2.duration = 0.5;
        animateImage2.fillMode = kCAFillModeForwards;
        animateImage2.removedOnCompletion = NO;
        [layer2 addAnimation: animateImage2 forKey: @"halfAnimatedImage"];

        return;
    }
}

-(void) rotateView1
{
    UIImageView *firstImage=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton];
    CALayer *layer =  firstImage.layer;
    layer.anchorPoint = CGPointMake(0.5f, 0.5f);
    CABasicAnimation *animateImage = [CABasicAnimation animationWithKeyPath: @"transform"];
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = 1.0/-900.0; 
    self.view.layer.transform = transform;
    layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
    [animateImage setDelegate:self ];
    animateImage.repeatCount = 0.5;
    animateImage.toValue = [NSValue valueWithCATransform3D: transform];
    animateImage.duration = 0.5;
    animateImage.fillMode = kCAFillModeForwards;
    animateImage.removedOnCompletion = NO;
    animateImage.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [animateImage setValue:@"SecondImage" forKey:@"halfAnimatedImage"];
    [layer addAnimation: animateImage forKey: @"halfAnimatedImage"];
}
-(void) rotateView2
{
    UIImageView *firstImage=(UIImageView *)[self.view viewWithTag:numberOfCurrentButton];
    CALayer *layer =  firstImage.layer;
    layer.anchorPoint = CGPointMake(0.5f, 0.5f);
    CABasicAnimation *animateImage = [CABasicAnimation animationWithKeyPath: @"transform"];
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = 1.0/-900.0; 
    self.view.layer.transform = transform;
    layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
    [animateImage setDelegate:self ];
    animateImage.repeatCount = 0.5;
    animateImage.toValue = [NSValue valueWithCATransform3D: transform];
    animateImage.duration = 0.5;
    animateImage.fillMode = kCAFillModeForwards;
    animateImage.removedOnCompletion = NO;
    animateImage.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [animateImage setValue:@"SecondImage" forKey:@"halfAnimatedImage"];
    [layer addAnimation: animateImage forKey: @"halfAnimatedImage"];
}
-(IBAction) buttonRefresh:(UIButton *)sender
{
    numberOfCurrentButton=1; 
    [self rotateView1];
    numberOfCurrentButton=2;
    [self rotateView2];
}
-(IBAction)buttonClicked:(UIButton *)sender
{
    [self buttonRefresh:sender];

}

`` `

最佳回答

根据我的理解,你的按键点击动作会收到发件人(被点击的UI按钮),而您会旋转同样的按钮。

要旋转,您可以进行以下两种操作之一:

  1. Create an IBOutlet (not IBAction) for both the buttons. In your rotateButtons method, instead of just rotating the sender UIButton, rotate both these buttons.
  2. 如@lnafziger 上面的建议, 标记在 XIB 中带有数字的按钮。 在旋转按钮方法中, 旋转按钮可以同时旋转此按钮。 您可以使用以下代码获得每个UI按钮 :

    UIbutton *button1 = (UIButton *)[self.view viewWithTag:101];
    
问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签