Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Created June 29, 2018 10:21
Show Gist options
  • Save randhirraj3130/7b638e72b383ba9e79ad725c82c08bd5 to your computer and use it in GitHub Desktop.
Save randhirraj3130/7b638e72b383ba9e79ad725c82c08bd5 to your computer and use it in GitHub Desktop.
How to create card view in swift and objective c
#import <UIKit/UIKit.h>
@interface CardView : UIView
@end
#import "CardView.h"
static CGFloat radius = 2;
static int shadowOffsetWidth = 0;
static int shadowOffsetHeight = 3;
static float shadowOpacity = 0.5;
@implementation CardView
-(void)layoutSubviews{
self.layer.cornerRadius = radius;
UIBezierPath *shadowPath = [UIBezierPath
bezierPathWithRoundedRect: self.bounds
cornerRadius: radius];
self.layer.masksToBounds = false;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(shadowOffsetWidth, shadowOffsetHeight);
self.layer.shadowOpacity = shadowOpacity;
self.layer.shadowPath = shadowPath.CGPath;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
return [super initWithCoder:aDecoder];
}
@end
//
// cardView.swift
// Negoshe
//
// Created by Mahabir on 06/06/18.
// Copyright © 2018 iws. All rights reserved.
//
import UIKit
@IBDesignable
class cardView: UIView {
@IBInspectable var cornerRadius: CGFloat = 2
@IBInspectable var shadowOffsetWidth: Int = 0
@IBInspectable var shadowOffsetHeight: Int = 3
@IBInspectable var shadowColor: UIColor? = UIColor.black
@IBInspectable var shadowOpacity: Float = 0.5
override func layoutSubviews() {
layer.cornerRadius = cornerRadius
let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
layer.masksToBounds = false
layer.shadowColor = shadowColor?.cgColor
layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight);
layer.shadowOpacity = shadowOpacity
layer.shadowPath = shadowPath.cgPath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment