Skip to content

IhorShevchuk/TMFloatingButton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TMFloatingButton

With this simple project you can add and design your material round button like in Android apps.

Installation

Swift Package Manager

  1. Add this package as dependcy
    .package(url: "https://github.com/IhorShevchuk/TMFloatingButton", , from: "1.2.0")
  1. Import TMFloatingButton
import TMFloatingButton

Manual

  1. Add Folder TMFloatingButton to project
  2. Import file TMFloatingButton.h to files where buttons will be shown

Requirements

  • Xcode 11 or higher
  • iOS 13.0 or higher
  • ARC

Usage

  1. In some method (ex:viewDidLoad) init TMFloatingButton object and add it to view where it should appear using next code:
  TMFloatingButton *floatingModeEditButton = [[TMFloatingButton alloc] initWithSuperView: self.navigationController.view];
  1. Apply one of predifined styles using next code:
 [TMFloatingButton addModeEditStyleToButton:floatingModeEditButton];
  1. Add target and selector using next code:
 [floatingModeEditButton addTarget:self action:@selector(modeEditAction) forControlEvents:UIControlEventTouchUpInside];

Design own button

For now you can use next styles

  • Favorite Style - Star button

  • Mode Edit Style - Red button with pen

  • Message Style - New message button

How can I design my own?

You can use one of next methods to create your own style

  • To add state with text use next code:
    [customTextButton addStateWithText:@"Custom text" withAttributes:textAttributesDictionary andBackgroundColor:bgColor forName:@"CustomTextState" applyRightNow:YES];

  • To add state with icon use next code
    [customIconButton addStateWithIcon:[UIImage imageNamed:@"icon-image"] andBackgroundColor:bgColor forName:@"CustomIconState" applyRightNow:NO];
  • To add state with icon and text use next code
   [customIconAndTextButton addStateWithIcon:[UIImage imageNamed:@"icon-image"] andText:@"Custom text" withAttributes:textAttributesDictionary andBackgroundColor:bgColor forName:@"CustomTextAndIconState" applyRightNow:NO];
  • Or you can design your own UIView and set using next code
    [customViewButton addStateWithView:customView andBackgroundColor:bgColor forName:@"CustomViewState" applyRightNow:YES];
About ApplyRightNow and TextAttributesDictionary parameters
  • BOOL parameter ApplyRightNow is using when you need set this state immediatly after adding state
  • TextAttributesDictionary is NSDictionary of text attributes (ex:NSFontAttributeName,NSForegroundColorAttributeName etc.)

Why I need to create states?

Sometimes button can change states during the app runing. Example add to favorites button that indicates if object(artilce,words,tags) added to favorites or not. And you can easy set this states with initing and after that just change states in different places in code

[button setButtonState:@"myState2"];
How can I show UIActivityIndicatorView while button's state is changing?

You can simply show UIActivityIndicatorView using this code:

    [floatingButton animateActivityIndicatorStart:YES];
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
       [floatingButton setButtonState:@"saved"];
       [floatingButton animateActivityIndicatorStart:NO];
        });

See example code for more details

TODO
  • Ability to move button on long tap
  • Support SwiftUI

License

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.