📜  UIView Shake (1)

📅  最后修改于: 2023-12-03 15:05:42.685000             🧑  作者: Mango

UIView Shake

UIView Shake is a simple and lightweight library for adding a shake effect to a UIView in your iOS app.

Features
  • Easy to use
  • Customizable shake animation
  • Support for both Swift and Objective-C
  • Lightweight library
Installation
CocoaPods

Add the following to your Podfile:

pod 'UIViewShake'

Then, run the following command:

pod install
Manual

Alternatively, you can manually add the UIView+Shake.{h,m} files to your project.

Usage

First, import the UIView+Shake.h file in your view controller or module:

#import "UIView+Shake.h"

Then, call the shake method on the view you want to shake:

[self.view shake];

By default, the shake animation will be a short back-and-forth motion. You can customize the shake animation by passing in a ShakeOptions struct with the properties you want to change:

ShakeOptions options = {
    .direction = ShakeDirectionHorizontal,
    .duration = 0.5,
    .repeatCount = 2,
    .shakeSpeed = 0.03,
    .damping = 0.05,
    .initialVelocity = 5
};
[self.view shakeWithOptions:options completionHandler:nil];
API
UIView+Shake

shake

- (void)shake;

Shake the view with the default shake animation.

shakeWithOptions:completionHandler:

- (void)shakeWithOptions:(ShakeOptions)options
       completionHandler:(void (^)(void))completionHandler;

Shake the view with custom options.

ShakeDirection

typedef NS_ENUM(NSInteger, ShakeDirection) {
    ShakeDirectionHorizontal = 0,
    ShakeDirectionVertical = 1,
};

An enumeration of the possible directions for the shake animation.

ShakeOptions

typedef struct _ShakeOptions {
    ShakeDirection direction;
    NSTimeInterval duration;
    NSUInteger repeatCount;
    CGFloat shakeSpeed;
    CGFloat damping;
    CGFloat initialVelocity;
} ShakeOptions;

A struct that contains the options for customizing the shake animation.

  • direction - The direction of the shake animation (ShakeDirectionHorizontal or ShakeDirectionVertical).
  • duration - The duration of the shake animation in seconds.
  • repeatCount - The number of times to repeat the shake animation.
  • shakeSpeed - The speed of the shake animation.
  • damping - The damping ratio of the shake animation.
  • initialVelocity - The initial velocity of the shake animation.
Conclusion

UIView Shake is a simple and lightweight library for adding a shake effect to a UIView in your iOS app. It's easy to use and highly customizable. Give it a try!