---
title: magneticField()
framework: uikit
role: symbol
role_heading: Type Method
path: uikit/uifieldbehavior/magneticfield()
---

# magneticField()

Creates and returns a field behavior that interacts with charged items.

## Declaration

```swift
class func magneticField() -> Self
```

## Return Value

Return Value A field behavior object that applies a magnetic field to charged items.

## Discussion

Discussion The magnetic field behavior models a uniform magnetic field in the positive-z direction—that is, coming out of the screen. The amount of force applied by the field is modeled after the second part of the Lorentz equation (F = qvB). When the velocity of a charged item is perpendicular to the uniform magnetic field, the item feels a force normal to both the velocity and the field, resulting in a counter-clockwise rotation. Specifying a negative value for the strength of the field results in a clockwise rotation. You can use magnetic fields as a way to apply forces to an object that are based on charge instead of mass.

## See Also

### Getting the field behaviors

- [dragField()](uikit/uifieldbehavior/dragfield().md)
- [springField()](uikit/uifieldbehavior/springfield().md)
- [velocityField(direction:)](uikit/uifieldbehavior/velocityfield(direction:).md)
- [electricField()](uikit/uifieldbehavior/electricfield().md)
- [radialGravityField(position:)](uikit/uifieldbehavior/radialgravityfield(position:).md)
- [linearGravityField(direction:)](uikit/uifieldbehavior/lineargravityfield(direction:).md)
- [vortexField()](uikit/uifieldbehavior/vortexfield().md)
- [noiseField(smoothness:animationSpeed:)](uikit/uifieldbehavior/noisefield(smoothness:animationspeed:).md)
- [turbulenceField(smoothness:animationSpeed:)](uikit/uifieldbehavior/turbulencefield(smoothness:animationspeed:).md)
- [field(evaluationBlock:)](uikit/uifieldbehavior/field(evaluationblock:).md)
