About the animation that moves the image view little by little while pressing the button.

Asked 2 years ago, Updated 2 years ago, 119 views

[UIView animationWithDuration:0] 
                      delay:0
                    options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
         self.view.transform=CGAffineTransformMakeTranslation(0,0);}
completion:^ 
{
}];

I understand that the above animation writing is not suitable for stopping in the middle.

This is sender from UIControlEventTouchDown using the storyboard

-(IBAction) botan:(UIButton*)sender{
    CGRect frame = self.viewname.frame;
    frame.origin.x + = 5;
    [self.viewname setFrame:frame];
}

How can I repeat this as long as the button is pressed?
I don't think it's smooth if I could repeat it, but how can I move it smoothly?

I'm a beginner, so I'm going to memorize it by touching it.
Thank you for your cooperation

objective-c iphone xcode6 storyboard uikit

2022-09-30 20:34

2 Answers

The first thing that comes to mind when it comes to animation is UIView's method group and CAAnimation framework, which are intended to be used for visual effects such as sliding the view up and down, tapping an object, or responding to user interaction.
iOS 7 adds a framework called UIDynamics, which provides a larger range of animations to the UIView subclass.This uses a Graphic Processing Unit (GPU) instead of a CPU, so you can create a smooth animation without having to worry about the machine.

#import "ViewController.h"

@ interface ViewController()

// Image View for Animation, ImageView
@property(weak, nonatomic) IBOutlet UIImageView* imageView;
// UIDynamicAnimator, a class that adds animation to the UIView
@property(nonatomic) UIDynamicAnimator*animator;

@end

@implementation ViewController

- (void) viewDidLoad{
    superviewDidLoad;

    // Include animation in the ViewController base view.
    self.animator=[[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}

// Tap anywhere on the screen.
- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event {
    // Generate classes that define behavior.Registered items (UIView subclasses) following this behavior in an array (NSArray).
    UIDynamicItemBehavior*moveBehavior=[[UIDynamicItemBehavior alloc]initWithItems:@[self.imageView]];
    // Define "behavior" called linear motion. Specify direction, speed, and items to move.
    MoveBehavior addLinearVelocity: CGPointMake (0.0,50.0) forItem:self.imageView;
    // Start animation.
    [self.animator addBehavior:moveBehavior];
}

// Remove the tapped finger from the screen.
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event {
    // End of animation.
    [self.animator removeAllBehaviors];
}

@end

Verify that the imageView moves intermittently by tapping.
UIDynamics can also move in circles and allow gravity to cause the view to fall.You need to study a little bit, but you should be able to get more than the effort you put into it.


2022-09-30 20:34

For example, if you want to repeat it with a timer, it would look like this.

#import "ViewController.h"

@ interface ViewController()

@property(weak, nonatomic)IBOutlet UIView*viewname;
@property(strong, nonatomic)NSTimer*timer;

@end

@implementation ViewController

- (void) viewDidLoad{

  superviewDidLoad;
}

- (IBAction) didTouchDownButton: (id)sender {//TouchDown

  self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.01f // Speed is adjusted to this value, which is the repeat interval
                 target —self
                 selector —@selector(translateViewName:)
                 userInfo:nil
                 repeats:YES];
}

- (IBAction) didTouchUpOrCancel: (id) sender {//Touch Cancel, Touch Up Inside, Touch Up Outside

  [self.timer invalidate]; // Stop timer
  self.timer=nil;
}

- (void)translateViewName: (NSTimer*)timer{

  CGRect frame = self.viewname.frame;
  frame.origin.x+=1.0f;// Visually, moving less than or equal to 1 point should look smooth
  [self.viewname setFrame:frame];
}

@end

For example, using UILongPressGesure for long press detection, using UISscreen's -displayLinkWithTarget:selector: for repeated runs, and learning CALayer's implicit and explicit animations and model/presentation layer differences, you may be able to use different methods.


2022-09-30 20:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.